blob: 53bbf2fe7d4f3e53a150c08be017f3ebe28996e9 [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;
284 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
285 (const char *) &n, sizeof( n ) );
286
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100287 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
288 {
289 close( *fd );
290 ret = POLARSSL_ERR_NET_BIND_FAILED;
291 continue;
292 }
293
294 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
295 {
296 close( *fd );
297 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
298 continue;
299 }
300
301 /* I we ever get there, it's a success */
302 ret = 0;
303 break;
304 }
305
306 freeaddrinfo( addr_list );
307
308 return( ret );
309
310#else
311 /* Legacy IPv4-only version */
312
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100313 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 struct sockaddr_in server_addr;
315
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100316 if( ( ret = net_prepare() ) != 0 )
317 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Paul Bakkerbbc10072013-10-14 16:33:24 +0200319 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000320 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322 n = 1;
323 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
324 (const char *) &n, sizeof( n ) );
325
Paul Bakker37286a52013-03-06 16:55:11 +0100326 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 server_addr.sin_family = AF_INET;
328 server_addr.sin_port = net_htons( port );
329
330 if( bind_ip != NULL )
331 {
332 memset( c, 0, sizeof( c ) );
333 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
334
335 for( n = 0; n < 4; n++ )
336 if( c[n] < 0 || c[n] > 255 )
337 break;
338
339 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100340 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000341 ( (uint32_t) c[0] << 24 ) |
342 ( (uint32_t) c[1] << 16 ) |
343 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100344 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 }
346
347 if( bind( *fd, (struct sockaddr *) &server_addr,
348 sizeof( server_addr ) ) < 0 )
349 {
350 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000351 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 }
353
Paul Bakker192381a2011-05-20 12:31:31 +0000354 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 {
356 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000357 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 }
359
360 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100361#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000362}
363
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100364#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
365 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100366/*
367 * Check if the requested operation would be blocking on a non-blocking socket
368 * and thus 'failed' with a negative return value.
369 */
370static int net_would_block( int fd )
371{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100372 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100374}
Paul Bakker5121ce52009-01-03 21:22:43 +0000375#else
Paul Bakker80025412014-01-23 20:59:49 +0100376/*
377 * Check if the requested operation would be blocking on a non-blocking socket
378 * and thus 'failed' with a negative return value.
379 *
380 * Note: on a blocking socket this function always returns 0!
381 */
382static int net_would_block( int fd )
383{
384 /*
385 * Never return 'WOULD BLOCK' on a non-blocking socket
386 */
387 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
388 return( 0 );
389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 switch( errno )
391 {
392#if defined EAGAIN
393 case EAGAIN:
394#endif
395#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
396 case EWOULDBLOCK:
397#endif
398 return( 1 );
399 }
400 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401}
Paul Bakker80025412014-01-23 20:59:49 +0100402#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
404/*
405 * Accept a connection from a remote client
406 */
407int net_accept( int bind_fd, int *client_fd, void *client_ip )
408{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100409#if defined(POLARSSL_HAVE_IPV6)
410 struct sockaddr_storage client_addr;
411#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
Paul Bakker394c56f2011-12-20 12:19:03 +0000415#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
416 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 socklen_t n = (socklen_t) sizeof( client_addr );
418#else
419 int n = (int) sizeof( client_addr );
420#endif
421
Paul Bakkerbbc10072013-10-14 16:33:24 +0200422 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
423 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425 if( *client_fd < 0 )
426 {
Paul Bakker80025412014-01-23 20:59:49 +0100427 if( net_would_block( *client_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000428 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Paul Bakker40e46942009-01-03 21:51:57 +0000430 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 }
432
433 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100434 {
435#if defined(POLARSSL_HAVE_IPV6)
436 if( client_addr.ss_family == AF_INET )
437 {
438 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
439 memcpy( client_ip, &addr4->sin_addr.s_addr,
440 sizeof( addr4->sin_addr.s_addr ) );
441 }
442 else
443 {
444 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
445 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
446 sizeof( addr6->sin6_addr.s6_addr ) );
447 }
448#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 memcpy( client_ip, &client_addr.sin_addr.s_addr,
450 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100451#endif /* POLARSSL_HAVE_IPV6 */
452 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454 return( 0 );
455}
456
457/*
458 * Set the socket blocking or non-blocking
459 */
460int net_set_block( int fd )
461{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100462#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
463 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000464 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 return( ioctlsocket( fd, FIONBIO, &n ) );
466#else
467 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
468#endif
469}
470
471int net_set_nonblock( int fd )
472{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100473#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
474 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000475 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 return( ioctlsocket( fd, FIONBIO, &n ) );
477#else
478 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
479#endif
480}
481
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200482#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000483/*
484 * Portable usleep helper
485 */
486void net_usleep( unsigned long usec )
487{
488 struct timeval tv;
489 tv.tv_sec = 0;
490 tv.tv_usec = usec;
491 select( 0, NULL, NULL, NULL, &tv );
492}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200493#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/*
496 * Read at most 'len' characters
497 */
Paul Bakker23986e52011-04-24 08:57:21 +0000498int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100499{
Paul Bakker80025412014-01-23 20:59:49 +0100500 int fd = *((int *) ctx);
501 int ret = read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 if( ret < 0 )
504 {
Paul Bakker80025412014-01-23 20:59:49 +0100505 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000506 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100508#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
509 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000511 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512#else
513 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000514 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000517 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518#endif
519
Paul Bakker40e46942009-01-03 21:51:57 +0000520 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 }
522
523 return( ret );
524}
525
526/*
527 * Write at most 'len' characters
528 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000529int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000530{
Paul Bakker80025412014-01-23 20:59:49 +0100531 int fd = *((int *) ctx);
532 int ret = write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 if( ret < 0 )
535 {
Paul Bakker80025412014-01-23 20:59:49 +0100536 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000537 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100539#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
540 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000542 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543#else
544 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000545 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000548 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549#endif
550
Paul Bakker40e46942009-01-03 21:51:57 +0000551 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553
554 return( ret );
555}
556
557/*
558 * Gracefully close the connection
559 */
560void net_close( int fd )
561{
562 shutdown( fd, 2 );
563 close( fd );
564}
565
566#endif