blob: ff489864e410ef6bff510545ef83908ac2067710 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakkerfa9b1002013-07-03 15:31:03 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerfa6a6202013-10-28 18:48:30 +010032#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
33 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010035#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010036#ifdef _WIN32_WINNT
37#undef _WIN32_WINNT
38#endif
39/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010040#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010041#include <ws2tcpip.h>
42#endif
43
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010044#include <winsock2.h>
45#include <windows.h>
46
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010047#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000048#if defined(_WIN32_WCE)
49#pragma comment( lib, "ws2.lib" )
50#else
51#pragma comment( lib, "ws2_32.lib" )
52#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010053#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakkerf4f69682011-04-24 16:08:12 +000055#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
56#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#define close(fd) closesocket(fd)
58
59static int wsa_init_done = 0;
60
61#else
62
63#include <sys/types.h>
64#include <sys/socket.h>
65#include <netinet/in.h>
66#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020067#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000068#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020069#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070#include <unistd.h>
71#include <signal.h>
72#include <fcntl.h>
73#include <netdb.h>
74#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000075
Paul Bakker6a2f8572012-08-23 07:45:37 +000076#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
77 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000078#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010079#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
80 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000081#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000082#elif defined(sun)
83#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020084#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
85#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000086#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000087#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90#endif
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092#include <stdlib.h>
93#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020094
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010095#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
96 !defined(EFI32)
97#define snprintf _snprintf
98#endif
99
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200100#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200102#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100104#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000105#include <basetsd.h>
106typedef UINT32 uint32_t;
107#else
108#include <inttypes.h>
109#endif
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000112 * htons() is not always available.
113 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +0100114 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000116#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000117#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100118#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000119#else
Paul Bakker37286a52013-03-06 16:55:11 +0100120#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
121 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
122#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
123 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
124 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
125 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000128unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100129unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000130#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100131#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100134 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100136static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100138#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
139 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 WSADATA wsaData;
141
142 if( wsa_init_done == 0 )
143 {
144 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000145 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 wsa_init_done = 1;
148 }
149#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100150#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 signal( SIGPIPE, SIG_IGN );
152#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100153#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100154 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100155}
156
157/*
158 * Initiate a TCP connection with host:port
159 */
160int net_connect( int *fd, const char *host, int port )
161{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100162#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100163 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100164 struct addrinfo hints, *addr_list, *cur;
165 char port_str[6];
166
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100167 if( ( ret = net_prepare() ) != 0 )
168 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100169
170 /* getaddrinfo expects port as a string */
171 memset( port_str, 0, sizeof( port_str ) );
172 snprintf( port_str, sizeof( port_str ), "%d", port );
173
174 /* Do name resolution with both IPv6 and IPv4, but only TCP */
175 memset( &hints, 0, sizeof( hints ) );
176 hints.ai_family = AF_UNSPEC;
177 hints.ai_socktype = SOCK_STREAM;
178 hints.ai_protocol = IPPROTO_TCP;
179
180 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
181 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
182
183 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100184 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100185 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
186 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100187 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
188 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100189 if( *fd < 0 )
190 {
191 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
192 continue;
193 }
194
195 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
196 {
197 ret = 0;
198 break;
199 }
200
201 close( *fd );
202 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
203 }
204
205 freeaddrinfo( addr_list );
206
207 return( ret );
208
209#else
210 /* Legacy IPv4-only version */
211
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100212 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100213 struct sockaddr_in server_addr;
214 struct hostent *server_host;
215
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100216 if( ( ret = net_prepare() ) != 0 )
217 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000220 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Paul Bakkerbbc10072013-10-14 16:33:24 +0200222 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000223 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225 memcpy( (void *) &server_addr.sin_addr,
226 (void *) server_host->h_addr,
227 server_host->h_length );
228
229 server_addr.sin_family = AF_INET;
230 server_addr.sin_port = net_htons( port );
231
232 if( connect( *fd, (struct sockaddr *) &server_addr,
233 sizeof( server_addr ) ) < 0 )
234 {
235 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000236 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 }
238
239 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100240#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000241}
242
243/*
244 * Create a listening socket on bind_ip:port
245 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000246int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000247{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100248#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100249 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100250 struct addrinfo hints, *addr_list, *cur;
251 char port_str[6];
252
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100253 if( ( ret = net_prepare() ) != 0 )
254 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100255
256 /* getaddrinfo expects port as a string */
257 memset( port_str, 0, sizeof( port_str ) );
258 snprintf( port_str, sizeof( port_str ), "%d", port );
259
260 /* Bind to IPv6 and/or IPv4, but only in TCP */
261 memset( &hints, 0, sizeof( hints ) );
262 hints.ai_family = AF_UNSPEC;
263 hints.ai_socktype = SOCK_STREAM;
264 hints.ai_protocol = IPPROTO_TCP;
265 if( bind_ip == NULL )
266 hints.ai_flags = AI_PASSIVE;
267
268 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
269 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
270
271 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100272 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100273 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
274 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100275 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
276 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100277 if( *fd < 0 )
278 {
279 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
280 continue;
281 }
282
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100283 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200284 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
285 (const char *) &n, sizeof( n ) ) != 0 )
286 {
287 close( *fd );
288 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
289 continue;
290 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100291
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100292 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
293 {
294 close( *fd );
295 ret = POLARSSL_ERR_NET_BIND_FAILED;
296 continue;
297 }
298
299 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
300 {
301 close( *fd );
302 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
303 continue;
304 }
305
306 /* I we ever get there, it's a success */
307 ret = 0;
308 break;
309 }
310
311 freeaddrinfo( addr_list );
312
313 return( ret );
314
315#else
316 /* Legacy IPv4-only version */
317
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100318 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 struct sockaddr_in server_addr;
320
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100321 if( ( ret = net_prepare() ) != 0 )
322 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Paul Bakkerbbc10072013-10-14 16:33:24 +0200324 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000325 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 n = 1;
328 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
329 (const char *) &n, sizeof( n ) );
330
Paul Bakker37286a52013-03-06 16:55:11 +0100331 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 server_addr.sin_family = AF_INET;
333 server_addr.sin_port = net_htons( port );
334
335 if( bind_ip != NULL )
336 {
337 memset( c, 0, sizeof( c ) );
338 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
339
340 for( n = 0; n < 4; n++ )
341 if( c[n] < 0 || c[n] > 255 )
342 break;
343
344 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100345 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000346 ( (uint32_t) c[0] << 24 ) |
347 ( (uint32_t) c[1] << 16 ) |
348 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100349 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 }
351
352 if( bind( *fd, (struct sockaddr *) &server_addr,
353 sizeof( server_addr ) ) < 0 )
354 {
355 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000356 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 }
358
Paul Bakker192381a2011-05-20 12:31:31 +0000359 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 {
361 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000362 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 }
364
365 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100366#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000367}
368
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100369#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
370 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100371/*
372 * Check if the requested operation would be blocking on a non-blocking socket
373 * and thus 'failed' with a negative return value.
374 */
375static int net_would_block( int fd )
376{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100377 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100379}
Paul Bakker5121ce52009-01-03 21:22:43 +0000380#else
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 *
385 * Note: on a blocking socket this function always returns 0!
386 */
387static int net_would_block( int fd )
388{
389 /*
390 * Never return 'WOULD BLOCK' on a non-blocking socket
391 */
392 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
393 return( 0 );
394
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 switch( errno )
396 {
397#if defined EAGAIN
398 case EAGAIN:
399#endif
400#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
401 case EWOULDBLOCK:
402#endif
403 return( 1 );
404 }
405 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406}
Paul Bakker80025412014-01-23 20:59:49 +0100407#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
409/*
410 * Accept a connection from a remote client
411 */
412int net_accept( int bind_fd, int *client_fd, void *client_ip )
413{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100414#if defined(POLARSSL_HAVE_IPV6)
415 struct sockaddr_storage client_addr;
416#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100418#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Paul Bakker394c56f2011-12-20 12:19:03 +0000420#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
421 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 socklen_t n = (socklen_t) sizeof( client_addr );
423#else
424 int n = (int) sizeof( client_addr );
425#endif
426
Paul Bakkerbbc10072013-10-14 16:33:24 +0200427 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
428 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430 if( *client_fd < 0 )
431 {
Paul Bakker80025412014-01-23 20:59:49 +0100432 if( net_would_block( *client_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000433 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
Paul Bakker40e46942009-01-03 21:51:57 +0000435 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 }
437
438 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100439 {
440#if defined(POLARSSL_HAVE_IPV6)
441 if( client_addr.ss_family == AF_INET )
442 {
443 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
444 memcpy( client_ip, &addr4->sin_addr.s_addr,
445 sizeof( addr4->sin_addr.s_addr ) );
446 }
447 else
448 {
449 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
450 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
451 sizeof( addr6->sin6_addr.s6_addr ) );
452 }
453#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 memcpy( client_ip, &client_addr.sin_addr.s_addr,
455 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100456#endif /* POLARSSL_HAVE_IPV6 */
457 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459 return( 0 );
460}
461
462/*
463 * Set the socket blocking or non-blocking
464 */
465int net_set_block( int fd )
466{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100467#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
468 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000469 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 return( ioctlsocket( fd, FIONBIO, &n ) );
471#else
472 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
473#endif
474}
475
476int net_set_nonblock( int fd )
477{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100478#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
479 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000480 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 return( ioctlsocket( fd, FIONBIO, &n ) );
482#else
483 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
484#endif
485}
486
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200487#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000488/*
489 * Portable usleep helper
490 */
491void net_usleep( unsigned long usec )
492{
493 struct timeval tv;
494 tv.tv_sec = 0;
495 tv.tv_usec = usec;
496 select( 0, NULL, NULL, NULL, &tv );
497}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200498#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/*
501 * Read at most 'len' characters
502 */
Paul Bakker23986e52011-04-24 08:57:21 +0000503int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100504{
Paul Bakker80025412014-01-23 20:59:49 +0100505 int fd = *((int *) ctx);
506 int ret = read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 if( ret < 0 )
509 {
Paul Bakker80025412014-01-23 20:59:49 +0100510 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000511 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100513#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
514 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000516 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517#else
518 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000522 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523#endif
524
Paul Bakker40e46942009-01-03 21:51:57 +0000525 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 }
527
528 return( ret );
529}
530
531/*
532 * Write at most 'len' characters
533 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000534int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535{
Paul Bakker80025412014-01-23 20:59:49 +0100536 int fd = *((int *) ctx);
537 int ret = write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 if( ret < 0 )
540 {
Paul Bakker80025412014-01-23 20:59:49 +0100541 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000542 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100544#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
545 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000547 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548#else
549 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000550 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000553 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554#endif
555
Paul Bakker40e46942009-01-03 21:51:57 +0000556 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558
559 return( ret );
560}
561
562/*
563 * Gracefully close the connection
564 */
565void net_close( int fd )
566{
567 shutdown( fd, 2 );
568 close( fd );
569}
570
571#endif