blob: b9fe68d431086db0b16828351fe815272f698fd5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Manuel Pégourié-Gonnardf4acfe12014-09-17 10:56:54 +02002 * TCP/IP or UDP/IP networking functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
37 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010039#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010040#ifdef _WIN32_WINNT
41#undef _WIN32_WINNT
42#endif
43/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010044#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010045#include <ws2tcpip.h>
46#endif
47
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010048#include <winsock2.h>
49#include <windows.h>
50
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010051#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#if defined(_WIN32_WCE)
53#pragma comment( lib, "ws2.lib" )
54#else
55#pragma comment( lib, "ws2_32.lib" )
56#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010057#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakkerf4f69682011-04-24 16:08:12 +000059#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
60#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000061#define close(fd) closesocket(fd)
62
63static int wsa_init_done = 0;
64
Paul Bakkerdb20c102014-06-17 14:34:44 +020065#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67#include <sys/types.h>
68#include <sys/socket.h>
69#include <netinet/in.h>
70#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020071#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000074#include <unistd.h>
75#include <signal.h>
76#include <fcntl.h>
77#include <netdb.h>
78#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000079
Paul Bakker6a2f8572012-08-23 07:45:37 +000080#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000081 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000082#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010083#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
84 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000085#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000086#elif defined(sun)
87#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020088#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
89#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000090#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000091#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Paul Bakkerdb20c102014-06-17 14:34:44 +020094#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Paul Bakker5121ce52009-01-03 21:22:43 +000096#include <stdlib.h>
97#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020098
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010099#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
100 !defined(EFI32)
101#define snprintf _snprintf
102#endif
103
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200104#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100108#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000109#include <basetsd.h>
110typedef UINT32 uint32_t;
111#else
112#include <inttypes.h>
113#endif
114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000116 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
118 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200120#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
121 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000122#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100123#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000124#else
Paul Bakker37286a52013-03-06 16:55:11 +0100125#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
126 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
127#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
128 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
129 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
130 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000131#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakker66d5d072014-06-17 16:39:18 +0200133unsigned short net_htons( unsigned short n );
134unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000135#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100136#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100139 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100141static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100143#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
144 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 WSADATA wsaData;
146
147 if( wsa_init_done == 0 )
148 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200149 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000150 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 wsa_init_done = 1;
153 }
154#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100155#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 signal( SIGPIPE, SIG_IGN );
157#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100158#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100159 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100160}
161
162/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100163 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100164 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100165int net_connect( int *fd, const char *host, int port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100166{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100167#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100168 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100169 struct addrinfo hints, *addr_list, *cur;
170 char port_str[6];
171
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100172 if( ( ret = net_prepare() ) != 0 )
173 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100174
175 /* getaddrinfo expects port as a string */
176 memset( port_str, 0, sizeof( port_str ) );
177 snprintf( port_str, sizeof( port_str ), "%d", port );
178
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100179 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100180 memset( &hints, 0, sizeof( hints ) );
181 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100182 hints.ai_socktype = proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
183 hints.ai_protocol = proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100184
185 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
186 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
187
188 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100189 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100190 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
191 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100192 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
193 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100194 if( *fd < 0 )
195 {
196 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
197 continue;
198 }
199
200 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
201 {
202 ret = 0;
203 break;
204 }
205
206 close( *fd );
207 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
208 }
209
210 freeaddrinfo( addr_list );
211
212 return( ret );
213
214#else
215 /* Legacy IPv4-only version */
216
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100217 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100218 struct sockaddr_in server_addr;
219 struct hostent *server_host;
220
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100221 if( ( ret = net_prepare() ) != 0 )
222 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000225 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100227 if( ( *fd = (int) socket( AF_INET,
228 proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
229 proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000230 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 memcpy( (void *) &server_addr.sin_addr,
233 (void *) server_host->h_addr,
234 server_host->h_length );
235
236 server_addr.sin_family = AF_INET;
237 server_addr.sin_port = net_htons( port );
238
239 if( connect( *fd, (struct sockaddr *) &server_addr,
240 sizeof( server_addr ) ) < 0 )
241 {
242 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000243 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 }
245
246 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100247#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000248}
249
250/*
251 * Create a listening socket on bind_ip:port
252 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100253int net_bind( int *fd, const char *bind_ip, int port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000254{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100255#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100256 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100257 struct addrinfo hints, *addr_list, *cur;
258 char port_str[6];
259
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100260 if( ( ret = net_prepare() ) != 0 )
261 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100262
263 /* getaddrinfo expects port as a string */
264 memset( port_str, 0, sizeof( port_str ) );
265 snprintf( port_str, sizeof( port_str ), "%d", port );
266
267 /* Bind to IPv6 and/or IPv4, but only in TCP */
268 memset( &hints, 0, sizeof( hints ) );
269 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100270 hints.ai_socktype = proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
271 hints.ai_protocol = proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100272 if( bind_ip == NULL )
273 hints.ai_flags = AI_PASSIVE;
274
275 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
276 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
277
278 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100279 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100280 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
281 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100282 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
283 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100284 if( *fd < 0 )
285 {
286 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
287 continue;
288 }
289
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100290 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200291 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
292 (const char *) &n, sizeof( n ) ) != 0 )
293 {
294 close( *fd );
295 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
296 continue;
297 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100298
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100299 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
300 {
301 close( *fd );
302 ret = POLARSSL_ERR_NET_BIND_FAILED;
303 continue;
304 }
305
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100306 /* Listen only makes sense for TCP */
307 if( proto == NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100308 {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100309 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
310 {
311 close( *fd );
312 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
313 continue;
314 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100315 }
316
317 /* I we ever get there, it's a success */
318 ret = 0;
319 break;
320 }
321
322 freeaddrinfo( addr_list );
323
324 return( ret );
325
326#else
327 /* Legacy IPv4-only version */
328
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100329 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 struct sockaddr_in server_addr;
331
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100332 if( ( ret = net_prepare() ) != 0 )
333 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100335 if( ( *fd = (int) socket( AF_INET,
336 proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
337 proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000338 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 n = 1;
341 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
342 (const char *) &n, sizeof( n ) );
343
Paul Bakker37286a52013-03-06 16:55:11 +0100344 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 server_addr.sin_family = AF_INET;
346 server_addr.sin_port = net_htons( port );
347
348 if( bind_ip != NULL )
349 {
350 memset( c, 0, sizeof( c ) );
351 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
352
353 for( n = 0; n < 4; n++ )
354 if( c[n] < 0 || c[n] > 255 )
355 break;
356
357 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100358 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000359 ( (uint32_t) c[0] << 24 ) |
360 ( (uint32_t) c[1] << 16 ) |
361 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100362 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 }
364
365 if( bind( *fd, (struct sockaddr *) &server_addr,
366 sizeof( server_addr ) ) < 0 )
367 {
368 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000369 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 }
371
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100372 /* Listen only makes sense for TCP */
373 if( proto == NET_PROTO_TCP )
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100375 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
376 {
377 close( *fd );
378 return( POLARSSL_ERR_NET_LISTEN_FAILED );
379 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 }
381
382 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100383#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000384}
385
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100386#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
387 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100388/*
389 * Check if the requested operation would be blocking on a non-blocking socket
390 * and thus 'failed' with a negative return value.
391 */
392static int net_would_block( int fd )
393{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100394 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100396}
Paul Bakker5121ce52009-01-03 21:22:43 +0000397#else
Paul Bakker80025412014-01-23 20:59:49 +0100398/*
399 * Check if the requested operation would be blocking on a non-blocking socket
400 * and thus 'failed' with a negative return value.
401 *
402 * Note: on a blocking socket this function always returns 0!
403 */
404static int net_would_block( int fd )
405{
406 /*
407 * Never return 'WOULD BLOCK' on a non-blocking socket
408 */
409 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
410 return( 0 );
411
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 switch( errno )
413 {
414#if defined EAGAIN
415 case EAGAIN:
416#endif
417#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
418 case EWOULDBLOCK:
419#endif
420 return( 1 );
421 }
422 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200424#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/*
427 * Accept a connection from a remote client
428 */
429int net_accept( int bind_fd, int *client_fd, void *client_ip )
430{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100431 int ret;
432 int type;
433
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100434#if defined(POLARSSL_HAVE_IPV6)
435 struct sockaddr_storage client_addr;
436#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100438#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
Paul Bakker394c56f2011-12-20 12:19:03 +0000440#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
441 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100443 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444#else
445 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100446 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447#endif
448
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100449 /* Is this a TCP or UDP socket? */
450 if( getsockopt( bind_fd, SOL_SOCKET, SO_TYPE, &type, &type_len ) != 0 ||
451 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
452 {
453 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
454 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100456 if( type == SOCK_STREAM )
457 {
458 /* TCP: actual accept() */
459 ret = *client_fd = (int) accept( bind_fd,
460 (struct sockaddr *) &client_addr, &n );
461 }
462 else
463 {
464 /* UDP: wait for a message, but keep it in the queue */
465 char buf[1] = { 0 };
466
Manuel Pégourié-Gonnardf3c500f2015-01-12 19:02:15 +0100467 ret = recvfrom( bind_fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100468 (struct sockaddr *) &client_addr, &n );
469 }
470
471 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200473 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000474 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
Paul Bakker40e46942009-01-03 21:51:57 +0000476 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 }
478
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100479 /* UDP: hijack the listening socket for communicating with the client */
480 if( type != SOCK_STREAM )
481 {
482 if( connect( bind_fd, (struct sockaddr *) &client_addr, n ) != 0 )
483 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
484
485 *client_fd = bind_fd;
486 }
487
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100489 {
490#if defined(POLARSSL_HAVE_IPV6)
491 if( client_addr.ss_family == AF_INET )
492 {
493 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
494 memcpy( client_ip, &addr4->sin_addr.s_addr,
495 sizeof( addr4->sin_addr.s_addr ) );
496 }
497 else
498 {
499 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
500 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
501 sizeof( addr6->sin6_addr.s6_addr ) );
502 }
503#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 memcpy( client_ip, &client_addr.sin_addr.s_addr,
505 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100506#endif /* POLARSSL_HAVE_IPV6 */
507 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 return( 0 );
510}
511
512/*
513 * Set the socket blocking or non-blocking
514 */
515int net_set_block( int fd )
516{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100517#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
518 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000519 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 return( ioctlsocket( fd, FIONBIO, &n ) );
521#else
522 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
523#endif
524}
525
526int net_set_nonblock( int fd )
527{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100528#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
529 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000530 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 return( ioctlsocket( fd, FIONBIO, &n ) );
532#else
533 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
534#endif
535}
536
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200537#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000538/*
539 * Portable usleep helper
540 */
541void net_usleep( unsigned long usec )
542{
543 struct timeval tv;
544 tv.tv_sec = 0;
Paul Bakker82788fb2014-10-20 13:59:19 +0200545#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
546 ( defined(__APPLE__) && defined(__MACH__) ) )
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200547 tv.tv_usec = (suseconds_t) usec;
548#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 tv.tv_usec = usec;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200550#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 select( 0, NULL, NULL, NULL, &tv );
552}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200553#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/*
556 * Read at most 'len' characters
557 */
Paul Bakker23986e52011-04-24 08:57:21 +0000558int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100559{
Paul Bakker80025412014-01-23 20:59:49 +0100560 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200561 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 if( ret < 0 )
564 {
Paul Bakker80025412014-01-23 20:59:49 +0100565 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000566 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100568#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
569 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000571 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572#else
573 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000574 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000577 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578#endif
579
Paul Bakker40e46942009-01-03 21:51:57 +0000580 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 }
582
583 return( ret );
584}
585
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200586#if defined(POLARSSL_HAVE_TIME)
587/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200588 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200589 */
590int net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200591 uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200592{
593 int ret;
594 struct timeval tv;
595 fd_set read_fds;
596 int fd = *((int *) ctx);
597
598 FD_ZERO( &read_fds );
599 FD_SET( fd, &read_fds );
600
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200601 tv.tv_sec = timeout / 1000;
602 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200603
604 ret = select( fd + 1, &read_fds, NULL, NULL, &tv );
605
606 /* Zero fds ready means we timed out */
607 if( ret == 0 )
608 return( POLARSSL_ERR_NET_TIMEOUT );
609
610 if( ret < 0 )
611 {
612#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
613 !defined(EFI32)
614 if( WSAGetLastError() == WSAEINTR )
615 return( POLARSSL_ERR_NET_WANT_READ );
616#else
617 if( errno == EINTR )
618 return( POLARSSL_ERR_NET_WANT_READ );
619#endif
620
621 return( POLARSSL_ERR_NET_RECV_FAILED );
622 }
623
624 /* This call will not block */
625 return( net_recv( ctx, buf, len ) );
626}
627#endif /* POLARSSL_HAVE_TIME */
628
Paul Bakker5121ce52009-01-03 21:22:43 +0000629/*
630 * Write at most 'len' characters
631 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000632int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000633{
Paul Bakker80025412014-01-23 20:59:49 +0100634 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200635 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 if( ret < 0 )
638 {
Paul Bakker80025412014-01-23 20:59:49 +0100639 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000640 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100642#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
643 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000645 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646#else
647 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000648 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
650 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000651 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652#endif
653
Paul Bakker40e46942009-01-03 21:51:57 +0000654 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
656
657 return( ret );
658}
659
660/*
661 * Gracefully close the connection
662 */
663void net_close( int fd )
664{
665 shutdown( fd, 2 );
666 close( fd );
667}
668
Paul Bakker9af723c2014-05-01 13:03:14 +0200669#endif /* POLARSSL_NET_C */