blob: 36fd06da6e1635c132b0bc2acb97ae16bfe592c2 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/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
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakkerfa6a6202013-10-28 18:48:30 +010033#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
34 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010036#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010037#ifdef _WIN32_WINNT
38#undef _WIN32_WINNT
39#endif
40/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010041#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010042#include <ws2tcpip.h>
43#endif
44
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010045#include <winsock2.h>
46#include <windows.h>
47
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010048#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#if defined(_WIN32_WCE)
50#pragma comment( lib, "ws2.lib" )
51#else
52#pragma comment( lib, "ws2_32.lib" )
53#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010054#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkerf4f69682011-04-24 16:08:12 +000056#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
57#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000058#define close(fd) closesocket(fd)
59
60static int wsa_init_done = 0;
61
Paul Bakkerdb20c102014-06-17 14:34:44 +020062#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64#include <sys/types.h>
65#include <sys/socket.h>
66#include <netinet/in.h>
67#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020068#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000069#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <unistd.h>
72#include <signal.h>
73#include <fcntl.h>
74#include <netdb.h>
75#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000076
Paul Bakker6a2f8572012-08-23 07:45:37 +000077#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffera26a0052014-04-22 20:16:15 +000078 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000079#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010080#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
81 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000082#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000083#elif defined(sun)
84#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020085#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
86#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000087#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000088#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakkerdb20c102014-06-17 14:34:44 +020091#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakker5121ce52009-01-03 21:22:43 +000093#include <stdlib.h>
94#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020095
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010096#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
97 !defined(EFI32)
98#define snprintf _snprintf
99#endif
100
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200101#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200103#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100105#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000106#include <basetsd.h>
107typedef UINT32 uint32_t;
108#else
109#include <inttypes.h>
110#endif
111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000113 * htons() is not always available.
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200114 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and
115 * __BIG_ENDIAN to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200117#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
118 __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000119#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100120#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000121#else
Paul Bakker37286a52013-03-06 16:55:11 +0100122#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
123 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
124#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
125 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
126 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
127 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000128#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Paul Bakker66d5d072014-06-17 16:39:18 +0200130unsigned short net_htons( unsigned short n );
131unsigned long net_htonl( unsigned long n );
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000132#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100133#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100136 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100138static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100140#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
141 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 WSADATA wsaData;
143
144 if( wsa_init_done == 0 )
145 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200146 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000147 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 wsa_init_done = 1;
150 }
151#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100152#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 signal( SIGPIPE, SIG_IGN );
154#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100155#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100156 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100157}
158
159/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100160 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100161 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100162int net_connect( int *fd, const char *host, int port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100163{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100164#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100165 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100166 struct addrinfo hints, *addr_list, *cur;
167 char port_str[6];
168
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100169 if( ( ret = net_prepare() ) != 0 )
170 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100171
172 /* getaddrinfo expects port as a string */
173 memset( port_str, 0, sizeof( port_str ) );
174 snprintf( port_str, sizeof( port_str ), "%d", port );
175
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100176 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100177 memset( &hints, 0, sizeof( hints ) );
178 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100179 hints.ai_socktype = proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
180 hints.ai_protocol = proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100181
182 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
183 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
184
185 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100186 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
188 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100189 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
190 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100191 if( *fd < 0 )
192 {
193 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
194 continue;
195 }
196
197 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
198 {
199 ret = 0;
200 break;
201 }
202
203 close( *fd );
204 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
205 }
206
207 freeaddrinfo( addr_list );
208
209 return( ret );
210
211#else
212 /* Legacy IPv4-only version */
213
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100214 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100215 struct sockaddr_in server_addr;
216 struct hostent *server_host;
217
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100218 if( ( ret = net_prepare() ) != 0 )
219 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000222 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100224 if( ( *fd = (int) socket( AF_INET,
225 proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
226 proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000227 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 memcpy( (void *) &server_addr.sin_addr,
230 (void *) server_host->h_addr,
231 server_host->h_length );
232
233 server_addr.sin_family = AF_INET;
234 server_addr.sin_port = net_htons( port );
235
236 if( connect( *fd, (struct sockaddr *) &server_addr,
237 sizeof( server_addr ) ) < 0 )
238 {
239 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000240 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 }
242
243 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100244#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000245}
246
247/*
248 * Create a listening socket on bind_ip:port
249 */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100250int net_bind( int *fd, const char *bind_ip, int port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100252#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100253 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100254 struct addrinfo hints, *addr_list, *cur;
255 char port_str[6];
256
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100257 if( ( ret = net_prepare() ) != 0 )
258 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100259
260 /* getaddrinfo expects port as a string */
261 memset( port_str, 0, sizeof( port_str ) );
262 snprintf( port_str, sizeof( port_str ), "%d", port );
263
264 /* Bind to IPv6 and/or IPv4, but only in TCP */
265 memset( &hints, 0, sizeof( hints ) );
266 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100267 hints.ai_socktype = proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
268 hints.ai_protocol = proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269 if( bind_ip == NULL )
270 hints.ai_flags = AI_PASSIVE;
271
272 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
273 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
274
275 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100276 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100277 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
278 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100279 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
280 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100281 if( *fd < 0 )
282 {
283 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
284 continue;
285 }
286
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100287 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200288 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
289 (const char *) &n, sizeof( n ) ) != 0 )
290 {
291 close( *fd );
292 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
293 continue;
294 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100295
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100296 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
297 {
298 close( *fd );
299 ret = POLARSSL_ERR_NET_BIND_FAILED;
300 continue;
301 }
302
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100303 /* Listen only makes sense for TCP */
304 if( proto == NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100305 {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100306 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
307 {
308 close( *fd );
309 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
310 continue;
311 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100312 }
313
314 /* I we ever get there, it's a success */
315 ret = 0;
316 break;
317 }
318
319 freeaddrinfo( addr_list );
320
321 return( ret );
322
323#else
324 /* Legacy IPv4-only version */
325
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100326 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 struct sockaddr_in server_addr;
328
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100329 if( ( ret = net_prepare() ) != 0 )
330 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100332 if( ( *fd = (int) socket( AF_INET,
333 proto == NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
334 proto == NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000335 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 n = 1;
338 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
339 (const char *) &n, sizeof( n ) );
340
Paul Bakker37286a52013-03-06 16:55:11 +0100341 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 server_addr.sin_family = AF_INET;
343 server_addr.sin_port = net_htons( port );
344
345 if( bind_ip != NULL )
346 {
347 memset( c, 0, sizeof( c ) );
348 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
349
350 for( n = 0; n < 4; n++ )
351 if( c[n] < 0 || c[n] > 255 )
352 break;
353
354 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100355 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000356 ( (uint32_t) c[0] << 24 ) |
357 ( (uint32_t) c[1] << 16 ) |
358 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100359 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 }
361
362 if( bind( *fd, (struct sockaddr *) &server_addr,
363 sizeof( server_addr ) ) < 0 )
364 {
365 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000366 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 }
368
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100369 /* Listen only makes sense for TCP */
370 if( proto == NET_PROTO_TCP )
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100372 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
373 {
374 close( *fd );
375 return( POLARSSL_ERR_NET_LISTEN_FAILED );
376 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 }
378
379 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100380#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000381}
382
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100383#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
384 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100385/*
386 * Check if the requested operation would be blocking on a non-blocking socket
387 * and thus 'failed' with a negative return value.
388 */
389static int net_would_block( int fd )
390{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100391 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100393}
Paul Bakker5121ce52009-01-03 21:22:43 +0000394#else
Paul Bakker80025412014-01-23 20:59:49 +0100395/*
396 * Check if the requested operation would be blocking on a non-blocking socket
397 * and thus 'failed' with a negative return value.
398 *
399 * Note: on a blocking socket this function always returns 0!
400 */
401static int net_would_block( int fd )
402{
403 /*
404 * Never return 'WOULD BLOCK' on a non-blocking socket
405 */
406 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
407 return( 0 );
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 switch( errno )
410 {
411#if defined EAGAIN
412 case EAGAIN:
413#endif
414#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
415 case EWOULDBLOCK:
416#endif
417 return( 1 );
418 }
419 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200421#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423/*
424 * Accept a connection from a remote client
425 */
426int net_accept( int bind_fd, int *client_fd, void *client_ip )
427{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100428 int ret;
429 int type;
430
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100431#if defined(POLARSSL_HAVE_IPV6)
432 struct sockaddr_storage client_addr;
433#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100435#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
Paul Bakker394c56f2011-12-20 12:19:03 +0000437#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
438 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100440 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441#else
442 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100443 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444#endif
445
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100446 /* Is this a TCP or UDP socket? */
447 if( getsockopt( bind_fd, SOL_SOCKET, SO_TYPE, &type, &type_len ) != 0 ||
448 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
449 {
450 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
451 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100453 if( type == SOCK_STREAM )
454 {
455 /* TCP: actual accept() */
456 ret = *client_fd = (int) accept( bind_fd,
457 (struct sockaddr *) &client_addr, &n );
458 }
459 else
460 {
461 /* UDP: wait for a message, but keep it in the queue */
462 char buf[1] = { 0 };
463
Manuel Pégourié-Gonnardf3c500f2015-01-12 19:02:15 +0100464 ret = recvfrom( bind_fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100465 (struct sockaddr *) &client_addr, &n );
466 }
467
468 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200470 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000471 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakker40e46942009-01-03 21:51:57 +0000473 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 }
475
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100476 /* UDP: hijack the listening socket for communicating with the client */
477 if( type != SOCK_STREAM )
478 {
479 if( connect( bind_fd, (struct sockaddr *) &client_addr, n ) != 0 )
480 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
481
482 *client_fd = bind_fd;
483 }
484
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100486 {
487#if defined(POLARSSL_HAVE_IPV6)
488 if( client_addr.ss_family == AF_INET )
489 {
490 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
491 memcpy( client_ip, &addr4->sin_addr.s_addr,
492 sizeof( addr4->sin_addr.s_addr ) );
493 }
494 else
495 {
496 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
497 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
498 sizeof( addr6->sin6_addr.s6_addr ) );
499 }
500#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 memcpy( client_ip, &client_addr.sin_addr.s_addr,
502 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100503#endif /* POLARSSL_HAVE_IPV6 */
504 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
506 return( 0 );
507}
508
509/*
510 * Set the socket blocking or non-blocking
511 */
512int net_set_block( int fd )
513{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100514#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
515 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000516 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 return( ioctlsocket( fd, FIONBIO, &n ) );
518#else
519 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
520#endif
521}
522
523int net_set_nonblock( int fd )
524{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100525#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
526 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000527 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 return( ioctlsocket( fd, FIONBIO, &n ) );
529#else
530 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
531#endif
532}
533
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200534#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000535/*
536 * Portable usleep helper
537 */
538void net_usleep( unsigned long usec )
539{
540 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100541 tv.tv_sec = usec / 1000000;
Paul Bakker82788fb2014-10-20 13:59:19 +0200542#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
543 ( defined(__APPLE__) && defined(__MACH__) ) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100544 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200545#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100546 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 select( 0, NULL, NULL, NULL, &tv );
549}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200550#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552/*
553 * Read at most 'len' characters
554 */
Paul Bakker23986e52011-04-24 08:57:21 +0000555int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100556{
Paul Bakker80025412014-01-23 20:59:49 +0100557 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200558 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 if( ret < 0 )
561 {
Paul Bakker80025412014-01-23 20:59:49 +0100562 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000563 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100565#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
566 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000568 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569#else
570 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000571 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000574 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575#endif
576
Paul Bakker40e46942009-01-03 21:51:57 +0000577 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 }
579
580 return( ret );
581}
582
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200583#if defined(POLARSSL_HAVE_TIME)
584/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200585 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200586 */
587int net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200588 uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200589{
590 int ret;
591 struct timeval tv;
592 fd_set read_fds;
593 int fd = *((int *) ctx);
594
595 FD_ZERO( &read_fds );
596 FD_SET( fd, &read_fds );
597
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200598 tv.tv_sec = timeout / 1000;
599 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200600
601 ret = select( fd + 1, &read_fds, NULL, NULL, &tv );
602
603 /* Zero fds ready means we timed out */
604 if( ret == 0 )
605 return( POLARSSL_ERR_NET_TIMEOUT );
606
607 if( ret < 0 )
608 {
609#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
610 !defined(EFI32)
611 if( WSAGetLastError() == WSAEINTR )
612 return( POLARSSL_ERR_NET_WANT_READ );
613#else
614 if( errno == EINTR )
615 return( POLARSSL_ERR_NET_WANT_READ );
616#endif
617
618 return( POLARSSL_ERR_NET_RECV_FAILED );
619 }
620
621 /* This call will not block */
622 return( net_recv( ctx, buf, len ) );
623}
624#endif /* POLARSSL_HAVE_TIME */
625
Paul Bakker5121ce52009-01-03 21:22:43 +0000626/*
627 * Write at most 'len' characters
628 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000629int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000630{
Paul Bakker80025412014-01-23 20:59:49 +0100631 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200632 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634 if( ret < 0 )
635 {
Paul Bakker80025412014-01-23 20:59:49 +0100636 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000637 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100639#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
640 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000642 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643#else
644 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000645 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000648 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000649#endif
650
Paul Bakker40e46942009-01-03 21:51:57 +0000651 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 }
653
654 return( ret );
655}
656
657/*
658 * Gracefully close the connection
659 */
660void net_close( int fd )
661{
662 shutdown( fd, 2 );
663 close( fd );
664}
665
Paul Bakker9af723c2014-05-01 13:03:14 +0200666#endif /* POLARSSL_NET_C */