blob: 24dd95e43776726bfb8041afcad7adb513a76d6e [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é-Gonnard13211352013-12-17 17:38:55 +010036#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010037#include <ws2tcpip.h>
38#endif
39
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010040#include <winsock2.h>
41#include <windows.h>
42
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010043#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000044#if defined(_WIN32_WCE)
45#pragma comment( lib, "ws2.lib" )
46#else
47#pragma comment( lib, "ws2_32.lib" )
48#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010049#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakkerf4f69682011-04-24 16:08:12 +000051#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
52#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000053#define close(fd) closesocket(fd)
54
55static int wsa_init_done = 0;
56
57#else
58
59#include <sys/types.h>
60#include <sys/socket.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020063#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000064#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000066#include <unistd.h>
67#include <signal.h>
68#include <fcntl.h>
69#include <netdb.h>
70#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000071
Paul Bakker6a2f8572012-08-23 07:45:37 +000072#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
73 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000074#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010075#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
76 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000077#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000078#elif defined(sun)
79#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020080#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
81#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000082#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000083#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86#endif
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088#include <stdlib.h>
89#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020090
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010091#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
92 !defined(EFI32)
93#define snprintf _snprintf
94#endif
95
Paul Bakkerfa9b1002013-07-03 15:31:03 +020096#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000097#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100100#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000101#include <basetsd.h>
102typedef UINT32 uint32_t;
103#else
104#include <inttypes.h>
105#endif
106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000108 * htons() is not always available.
109 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +0100110 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000112#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000113#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100114#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000115#else
Paul Bakker37286a52013-03-06 16:55:11 +0100116#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
117 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
118#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
119 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
120 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
121 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000122#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000124unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100125unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000126#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100127#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100130 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100132static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100134#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
135 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 WSADATA wsaData;
137
138 if( wsa_init_done == 0 )
139 {
140 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000141 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143 wsa_init_done = 1;
144 }
145#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100146#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 signal( SIGPIPE, SIG_IGN );
148#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100149#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100150 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100151}
152
153/*
154 * Initiate a TCP connection with host:port
155 */
156int net_connect( int *fd, const char *host, int port )
157{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100158#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100159 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100160 struct addrinfo hints, *addr_list, *cur;
161 char port_str[6];
162
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100163 if( ( ret = net_prepare() ) != 0 )
164 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100165
166 /* getaddrinfo expects port as a string */
167 memset( port_str, 0, sizeof( port_str ) );
168 snprintf( port_str, sizeof( port_str ), "%d", port );
169
170 /* Do name resolution with both IPv6 and IPv4, but only TCP */
171 memset( &hints, 0, sizeof( hints ) );
172 hints.ai_family = AF_UNSPEC;
173 hints.ai_socktype = SOCK_STREAM;
174 hints.ai_protocol = IPPROTO_TCP;
175
176 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
177 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
178
179 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100180 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100181 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
182 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100183 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
184 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100185 if( *fd < 0 )
186 {
187 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
188 continue;
189 }
190
191 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
192 {
193 ret = 0;
194 break;
195 }
196
197 close( *fd );
198 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
199 }
200
201 freeaddrinfo( addr_list );
202
203 return( ret );
204
205#else
206 /* Legacy IPv4-only version */
207
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100208 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100209 struct sockaddr_in server_addr;
210 struct hostent *server_host;
211
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100212 if( ( ret = net_prepare() ) != 0 )
213 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000216 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakkerbbc10072013-10-14 16:33:24 +0200218 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000219 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 memcpy( (void *) &server_addr.sin_addr,
222 (void *) server_host->h_addr,
223 server_host->h_length );
224
225 server_addr.sin_family = AF_INET;
226 server_addr.sin_port = net_htons( port );
227
228 if( connect( *fd, (struct sockaddr *) &server_addr,
229 sizeof( server_addr ) ) < 0 )
230 {
231 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000232 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 }
234
235 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100236#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000237}
238
239/*
240 * Create a listening socket on bind_ip:port
241 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000242int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100244#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100245 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100246 struct addrinfo hints, *addr_list, *cur;
247 char port_str[6];
248
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100249 if( ( ret = net_prepare() ) != 0 )
250 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100251
252 /* getaddrinfo expects port as a string */
253 memset( port_str, 0, sizeof( port_str ) );
254 snprintf( port_str, sizeof( port_str ), "%d", port );
255
256 /* Bind to IPv6 and/or IPv4, but only in TCP */
257 memset( &hints, 0, sizeof( hints ) );
258 hints.ai_family = AF_UNSPEC;
259 hints.ai_socktype = SOCK_STREAM;
260 hints.ai_protocol = IPPROTO_TCP;
261 if( bind_ip == NULL )
262 hints.ai_flags = AI_PASSIVE;
263
264 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
265 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
266
267 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100268 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
270 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100271 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
272 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100273 if( *fd < 0 )
274 {
275 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
276 continue;
277 }
278
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100279 n = 1;
280 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
281 (const char *) &n, sizeof( n ) );
282
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100283 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
284 {
285 close( *fd );
286 ret = POLARSSL_ERR_NET_BIND_FAILED;
287 continue;
288 }
289
290 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
291 {
292 close( *fd );
293 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
294 continue;
295 }
296
297 /* I we ever get there, it's a success */
298 ret = 0;
299 break;
300 }
301
302 freeaddrinfo( addr_list );
303
304 return( ret );
305
306#else
307 /* Legacy IPv4-only version */
308
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100309 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 struct sockaddr_in server_addr;
311
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100312 if( ( ret = net_prepare() ) != 0 )
313 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Paul Bakkerbbc10072013-10-14 16:33:24 +0200315 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000316 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 n = 1;
319 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
320 (const char *) &n, sizeof( n ) );
321
Paul Bakker37286a52013-03-06 16:55:11 +0100322 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 server_addr.sin_family = AF_INET;
324 server_addr.sin_port = net_htons( port );
325
326 if( bind_ip != NULL )
327 {
328 memset( c, 0, sizeof( c ) );
329 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
330
331 for( n = 0; n < 4; n++ )
332 if( c[n] < 0 || c[n] > 255 )
333 break;
334
335 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100336 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000337 ( (uint32_t) c[0] << 24 ) |
338 ( (uint32_t) c[1] << 16 ) |
339 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100340 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 }
342
343 if( bind( *fd, (struct sockaddr *) &server_addr,
344 sizeof( server_addr ) ) < 0 )
345 {
346 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000347 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 }
349
Paul Bakker192381a2011-05-20 12:31:31 +0000350 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 {
352 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000353 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 }
355
356 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100357#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000358}
359
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100360#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
361 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100362/*
363 * Check if the requested operation would be blocking on a non-blocking socket
364 * and thus 'failed' with a negative return value.
365 */
366static int net_would_block( int fd )
367{
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100369}
Paul Bakker5121ce52009-01-03 21:22:43 +0000370#else
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 *
375 * Note: on a blocking socket this function always returns 0!
376 */
377static int net_would_block( int fd )
378{
379 /*
380 * Never return 'WOULD BLOCK' on a non-blocking socket
381 */
382 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
383 return( 0 );
384
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 switch( errno )
386 {
387#if defined EAGAIN
388 case EAGAIN:
389#endif
390#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
391 case EWOULDBLOCK:
392#endif
393 return( 1 );
394 }
395 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000396}
Paul Bakker80025412014-01-23 20:59:49 +0100397#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399/*
400 * Accept a connection from a remote client
401 */
402int net_accept( int bind_fd, int *client_fd, void *client_ip )
403{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100404#if defined(POLARSSL_HAVE_IPV6)
405 struct sockaddr_storage client_addr;
406#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100408#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Paul Bakker394c56f2011-12-20 12:19:03 +0000410#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
411 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 socklen_t n = (socklen_t) sizeof( client_addr );
413#else
414 int n = (int) sizeof( client_addr );
415#endif
416
Paul Bakkerbbc10072013-10-14 16:33:24 +0200417 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
418 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 if( *client_fd < 0 )
421 {
Paul Bakker80025412014-01-23 20:59:49 +0100422 if( net_would_block( *client_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000423 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
Paul Bakker40e46942009-01-03 21:51:57 +0000425 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 }
427
428 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100429 {
430#if defined(POLARSSL_HAVE_IPV6)
431 if( client_addr.ss_family == AF_INET )
432 {
433 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
434 memcpy( client_ip, &addr4->sin_addr.s_addr,
435 sizeof( addr4->sin_addr.s_addr ) );
436 }
437 else
438 {
439 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
440 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
441 sizeof( addr6->sin6_addr.s6_addr ) );
442 }
443#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 memcpy( client_ip, &client_addr.sin_addr.s_addr,
445 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100446#endif /* POLARSSL_HAVE_IPV6 */
447 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449 return( 0 );
450}
451
452/*
453 * Set the socket blocking or non-blocking
454 */
455int net_set_block( int fd )
456{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100457#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
458 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000459 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 return( ioctlsocket( fd, FIONBIO, &n ) );
461#else
462 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
463#endif
464}
465
466int net_set_nonblock( int fd )
467{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100468#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
469 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000470 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 return( ioctlsocket( fd, FIONBIO, &n ) );
472#else
473 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
474#endif
475}
476
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200477#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000478/*
479 * Portable usleep helper
480 */
481void net_usleep( unsigned long usec )
482{
483 struct timeval tv;
484 tv.tv_sec = 0;
485 tv.tv_usec = usec;
486 select( 0, NULL, NULL, NULL, &tv );
487}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200488#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/*
491 * Read at most 'len' characters
492 */
Paul Bakker23986e52011-04-24 08:57:21 +0000493int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100494{
Paul Bakker80025412014-01-23 20:59:49 +0100495 int fd = *((int *) ctx);
496 int ret = read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 if( ret < 0 )
499 {
Paul Bakker80025412014-01-23 20:59:49 +0100500 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000501 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100503#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
504 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000506 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507#else
508 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000509 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000512 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513#endif
514
Paul Bakker40e46942009-01-03 21:51:57 +0000515 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 }
517
518 return( ret );
519}
520
521/*
522 * Write at most 'len' characters
523 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000524int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000525{
Paul Bakker80025412014-01-23 20:59:49 +0100526 int fd = *((int *) ctx);
527 int ret = write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 if( ret < 0 )
530 {
Paul Bakker80025412014-01-23 20:59:49 +0100531 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000532 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100534#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
535 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000537 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538#else
539 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000540 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
542 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000543 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544#endif
545
Paul Bakker40e46942009-01-03 21:51:57 +0000546 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 }
548
549 return( ret );
550}
551
552/*
553 * Gracefully close the connection
554 */
555void net_close( int fd )
556{
557 shutdown( fd, 2 );
558 close( fd );
559}
560
561#endif