blob: 1bd5160598620fd9bd229512de9823f4e73bb942 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
36 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010038#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010039#ifdef _WIN32_WINNT
40#undef _WIN32_WINNT
41#endif
42/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010043#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010044#include <ws2tcpip.h>
45#endif
46
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010047#include <winsock2.h>
48#include <windows.h>
49
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010050#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#if defined(_WIN32_WCE)
52#pragma comment( lib, "ws2.lib" )
53#else
54#pragma comment( lib, "ws2_32.lib" )
55#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010056#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakkerf4f69682011-04-24 16:08:12 +000058#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
59#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000060#define close(fd) closesocket(fd)
61
62static int wsa_init_done = 0;
63
Paul Bakkerdb20c102014-06-17 14:34:44 +020064#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66#include <sys/types.h>
67#include <sys/socket.h>
68#include <netinet/in.h>
69#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020072#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000073#include <unistd.h>
74#include <signal.h>
75#include <fcntl.h>
76#include <netdb.h>
77#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000078
Paul Bakker6a2f8572012-08-23 07:45:37 +000079#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000080 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000081#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010082#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
83 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000084#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000085#elif defined(sun)
86#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020087#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
88#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000089#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000090#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakkerdb20c102014-06-17 14:34:44 +020093#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakker5121ce52009-01-03 21:22:43 +000095#include <stdlib.h>
96#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020097
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010098#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
99 !defined(EFI32)
100#define snprintf _snprintf
101#endif
102
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200103#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100107#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000108#include <basetsd.h>
109typedef UINT32 uint32_t;
110#else
111#include <inttypes.h>
112#endif
113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000115 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200116 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
117 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200119#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
120 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000121#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100122#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000123#else
Paul Bakker37286a52013-03-06 16:55:11 +0100124#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
125 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
126#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
127 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
128 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
129 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000130#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Rich Evansa18b11f2015-01-30 10:58:35 +0000132#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000133#include "mbedtls/platform.h"
Rich Evansa18b11f2015-01-30 10:58:35 +0000134#else
135#define polarssl_snprintf snprintf
136#endif
137
Paul Bakker66d5d072014-06-17 16:39:18 +0200138unsigned short net_htons( unsigned short n );
139unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000140#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100141#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100144 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100146static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100148#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
149 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 WSADATA wsaData;
151
152 if( wsa_init_done == 0 )
153 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200154 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000155 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157 wsa_init_done = 1;
158 }
159#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100160#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 signal( SIGPIPE, SIG_IGN );
162#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100163#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100164 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100165}
166
167/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100168 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100169 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100170int net_connect( int *fd, const char *host, int port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100171{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100172#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100173 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100174 struct addrinfo hints, *addr_list, *cur;
175 char port_str[6];
176
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100177 if( ( ret = net_prepare() ) != 0 )
178 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100179
180 /* getaddrinfo expects port as a string */
181 memset( port_str, 0, sizeof( port_str ) );
Rich Evansa18b11f2015-01-30 10:58:35 +0000182 polarssl_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100183
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100184 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100185 memset( &hints, 0, sizeof( hints ) );
186 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100187 hints.ai_socktype = proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
188 hints.ai_protocol = proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100189
190 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
191 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
192
193 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100194 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100195 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
196 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100197 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
198 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100199 if( *fd < 0 )
200 {
201 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
202 continue;
203 }
204
205 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
206 {
207 ret = 0;
208 break;
209 }
210
211 close( *fd );
212 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
213 }
214
215 freeaddrinfo( addr_list );
216
217 return( ret );
218
219#else
220 /* Legacy IPv4-only version */
221
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100222 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100223 struct sockaddr_in server_addr;
224 struct hostent *server_host;
225
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100226 if( ( ret = net_prepare() ) != 0 )
227 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000230 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100232 if( ( *fd = (int) socket( AF_INET,
233 proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
234 proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000235 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237 memcpy( (void *) &server_addr.sin_addr,
238 (void *) server_host->h_addr,
239 server_host->h_length );
240
241 server_addr.sin_family = AF_INET;
242 server_addr.sin_port = net_htons( port );
243
244 if( connect( *fd, (struct sockaddr *) &server_addr,
245 sizeof( server_addr ) ) < 0 )
246 {
247 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000248 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 }
250
251 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100252#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000253}
254
255/*
256 * Create a listening socket on bind_ip:port
257 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100258int net_bind( int *fd, const char *bind_ip, int port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000259{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100260#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100261 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100262 struct addrinfo hints, *addr_list, *cur;
263 char port_str[6];
264
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100265 if( ( ret = net_prepare() ) != 0 )
266 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100267
268 /* getaddrinfo expects port as a string */
269 memset( port_str, 0, sizeof( port_str ) );
Rich Evansa18b11f2015-01-30 10:58:35 +0000270 polarssl_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100271
272 /* Bind to IPv6 and/or IPv4, but only in TCP */
273 memset( &hints, 0, sizeof( hints ) );
274 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100275 hints.ai_socktype = proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
276 hints.ai_protocol = proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100277 if( bind_ip == NULL )
278 hints.ai_flags = AI_PASSIVE;
279
280 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
281 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
282
283 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100284 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100285 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
286 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100287 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
288 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100289 if( *fd < 0 )
290 {
291 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
292 continue;
293 }
294
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100295 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200296 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
297 (const char *) &n, sizeof( n ) ) != 0 )
298 {
299 close( *fd );
300 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
301 continue;
302 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100303
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100304 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
305 {
306 close( *fd );
307 ret = POLARSSL_ERR_NET_BIND_FAILED;
308 continue;
309 }
310
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100311 /* Listen only makes sense for TCP */
312 if( proto == NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100313 {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100314 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
315 {
316 close( *fd );
317 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
318 continue;
319 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100320 }
321
322 /* I we ever get there, it's a success */
323 ret = 0;
324 break;
325 }
326
327 freeaddrinfo( addr_list );
328
329 return( ret );
330
331#else
332 /* Legacy IPv4-only version */
333
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100334 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 struct sockaddr_in server_addr;
336
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100337 if( ( ret = net_prepare() ) != 0 )
338 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100340 if( ( *fd = (int) socket( AF_INET,
341 proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
342 proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345 n = 1;
346 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
347 (const char *) &n, sizeof( n ) );
348
Paul Bakker37286a52013-03-06 16:55:11 +0100349 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 server_addr.sin_family = AF_INET;
351 server_addr.sin_port = net_htons( port );
352
353 if( bind_ip != NULL )
354 {
355 memset( c, 0, sizeof( c ) );
356 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
357
358 for( n = 0; n < 4; n++ )
359 if( c[n] < 0 || c[n] > 255 )
360 break;
361
362 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100363 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000364 ( (uint32_t) c[0] << 24 ) |
365 ( (uint32_t) c[1] << 16 ) |
366 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100367 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 }
369
370 if( bind( *fd, (struct sockaddr *) &server_addr,
371 sizeof( server_addr ) ) < 0 )
372 {
373 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000374 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 }
376
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100377 /* Listen only makes sense for TCP */
378 if( proto == NET_PROTO_TCP )
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100380 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
381 {
382 close( *fd );
383 return( POLARSSL_ERR_NET_LISTEN_FAILED );
384 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 }
386
387 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100388#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000389}
390
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100391#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
392 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100393/*
394 * Check if the requested operation would be blocking on a non-blocking socket
395 * and thus 'failed' with a negative return value.
396 */
397static int net_would_block( int fd )
398{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100399 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100401}
Paul Bakker5121ce52009-01-03 21:22:43 +0000402#else
Paul Bakker80025412014-01-23 20:59:49 +0100403/*
404 * Check if the requested operation would be blocking on a non-blocking socket
405 * and thus 'failed' with a negative return value.
406 *
407 * Note: on a blocking socket this function always returns 0!
408 */
409static int net_would_block( int fd )
410{
411 /*
412 * Never return 'WOULD BLOCK' on a non-blocking socket
413 */
414 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
415 return( 0 );
416
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 switch( errno )
418 {
419#if defined EAGAIN
420 case EAGAIN:
421#endif
422#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
423 case EWOULDBLOCK:
424#endif
425 return( 1 );
426 }
427 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200429#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431/*
432 * Accept a connection from a remote client
433 */
434int net_accept( int bind_fd, int *client_fd, void *client_ip )
435{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100436 int ret;
437 int type;
438
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100439#if defined(POLARSSL_HAVE_IPV6)
440 struct sockaddr_storage client_addr;
441#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100443#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
Paul Bakker394c56f2011-12-20 12:19:03 +0000445#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
446 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100448 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449#else
450 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100451 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452#endif
453
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100454 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard9325b262015-03-25 16:57:52 +0100455 if( getsockopt( bind_fd, SOL_SOCKET, SO_TYPE, (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100456 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
457 {
458 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
459 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100461 if( type == SOCK_STREAM )
462 {
463 /* TCP: actual accept() */
464 ret = *client_fd = (int) accept( bind_fd,
465 (struct sockaddr *) &client_addr, &n );
466 }
467 else
468 {
469 /* UDP: wait for a message, but keep it in the queue */
470 char buf[1] = { 0 };
471
Manuel Pégourié-Gonnardf3c500f2015-01-12 19:02:15 +0100472 ret = recvfrom( bind_fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100473 (struct sockaddr *) &client_addr, &n );
474 }
475
476 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200478 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000479 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
Paul Bakker40e46942009-01-03 21:51:57 +0000481 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 }
483
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100484 /* UDP: hijack the listening socket for communicating with the client */
485 if( type != SOCK_STREAM )
486 {
487 if( connect( bind_fd, (struct sockaddr *) &client_addr, n ) != 0 )
488 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
489
490 *client_fd = bind_fd;
491 }
492
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100494 {
495#if defined(POLARSSL_HAVE_IPV6)
496 if( client_addr.ss_family == AF_INET )
497 {
498 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
499 memcpy( client_ip, &addr4->sin_addr.s_addr,
500 sizeof( addr4->sin_addr.s_addr ) );
501 }
502 else
503 {
504 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
505 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
506 sizeof( addr6->sin6_addr.s6_addr ) );
507 }
508#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 memcpy( client_ip, &client_addr.sin_addr.s_addr,
510 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100511#endif /* POLARSSL_HAVE_IPV6 */
512 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 return( 0 );
515}
516
517/*
518 * Set the socket blocking or non-blocking
519 */
520int net_set_block( int fd )
521{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100522#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
523 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000524 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 return( ioctlsocket( fd, FIONBIO, &n ) );
526#else
527 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
528#endif
529}
530
531int net_set_nonblock( int fd )
532{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100533#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
534 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000535 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 return( ioctlsocket( fd, FIONBIO, &n ) );
537#else
538 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
539#endif
540}
541
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200542#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000543/*
544 * Portable usleep helper
545 */
546void net_usleep( unsigned long usec )
547{
548 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100549 tv.tv_sec = usec / 1000000;
Paul Bakker82788fb2014-10-20 13:59:19 +0200550#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
551 ( defined(__APPLE__) && defined(__MACH__) ) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100552 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200553#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100554 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200555#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 select( 0, NULL, NULL, NULL, &tv );
557}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200558#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560/*
561 * Read at most 'len' characters
562 */
Paul Bakker23986e52011-04-24 08:57:21 +0000563int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100564{
Paul Bakker80025412014-01-23 20:59:49 +0100565 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200566 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 if( ret < 0 )
569 {
Paul Bakker80025412014-01-23 20:59:49 +0100570 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000571 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100573#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
574 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000576 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577#else
578 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000579 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000582 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583#endif
584
Paul Bakker40e46942009-01-03 21:51:57 +0000585 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587
588 return( ret );
589}
590
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200591#if defined(POLARSSL_HAVE_TIME)
592/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200593 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200594 */
595int net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200596 uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200597{
598 int ret;
599 struct timeval tv;
600 fd_set read_fds;
601 int fd = *((int *) ctx);
602
603 FD_ZERO( &read_fds );
604 FD_SET( fd, &read_fds );
605
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200606 tv.tv_sec = timeout / 1000;
607 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200608
609 ret = select( fd + 1, &read_fds, NULL, NULL, &tv );
610
611 /* Zero fds ready means we timed out */
612 if( ret == 0 )
613 return( POLARSSL_ERR_NET_TIMEOUT );
614
615 if( ret < 0 )
616 {
617#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
618 !defined(EFI32)
619 if( WSAGetLastError() == WSAEINTR )
620 return( POLARSSL_ERR_NET_WANT_READ );
621#else
622 if( errno == EINTR )
623 return( POLARSSL_ERR_NET_WANT_READ );
624#endif
625
626 return( POLARSSL_ERR_NET_RECV_FAILED );
627 }
628
629 /* This call will not block */
630 return( net_recv( ctx, buf, len ) );
631}
632#endif /* POLARSSL_HAVE_TIME */
633
Paul Bakker5121ce52009-01-03 21:22:43 +0000634/*
635 * Write at most 'len' characters
636 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000637int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000638{
Paul Bakker80025412014-01-23 20:59:49 +0100639 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200640 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642 if( ret < 0 )
643 {
Paul Bakker80025412014-01-23 20:59:49 +0100644 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000645 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100647#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
648 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000650 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000651#else
652 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000653 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
655 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000656 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657#endif
658
Paul Bakker40e46942009-01-03 21:51:57 +0000659 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 }
661
662 return( ret );
663}
664
665/*
666 * Gracefully close the connection
667 */
668void net_close( int fd )
669{
670 shutdown( fd, 2 );
671 close( fd );
672}
673
Paul Bakker9af723c2014-05-01 13:03:14 +0200674#endif /* POLARSSL_NET_C */