blob: 0e5edc01e0ff5dfa354af0d1ea6a0b5881dd4e76 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +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
Paul Bakker40e46942009-01-03 21:51:57 +000023#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker40e46942009-01-03 21:51:57 +000025#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakkerff60ee62010-03-16 21:09:09 +000029#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
31#include <winsock2.h>
32#include <windows.h>
33
34#if defined(_WIN32_WCE)
35#pragma comment( lib, "ws2.lib" )
36#else
37#pragma comment( lib, "ws2_32.lib" )
38#endif
39
Paul Bakkerf4f69682011-04-24 16:08:12 +000040#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
41#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000042#define close(fd) closesocket(fd)
43
44static int wsa_init_done = 0;
45
46#else
47
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51#include <arpa/inet.h>
52#include <sys/time.h>
53#include <unistd.h>
54#include <signal.h>
55#include <fcntl.h>
56#include <netdb.h>
57#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000058
Paul Bakker6a2f8572012-08-23 07:45:37 +000059#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
Markus Pfeiffer55bdbc12014-04-22 20:16:15 +000060 defined(__DragonFly__)
Paul Bakker854963c2009-07-19 20:50:11 +000061#include <sys/endian.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000062#elif defined(__APPLE__)
63#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000064#elif defined(sun)
65#include <sys/isa_defs.h>
Paul Bakker854963c2009-07-19 20:50:11 +000066#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000067#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70#endif
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <stdlib.h>
73#include <stdio.h>
74#include <time.h>
75
Paul Bakker5c2364c2012-10-01 14:41:15 +000076#ifdef _MSC_VER
77#include <basetsd.h>
78typedef UINT32 uint32_t;
79#else
80#include <inttypes.h>
81#endif
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000084 * htons() is not always available.
85 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
86 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000087 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000088#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000089#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +010090#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000091#else
Paul Bakker37286a52013-03-06 16:55:11 +010092#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
93 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
94#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
95 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
96 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
97 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000100unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100101unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000102#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100103#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/*
106 * Initiate a TCP connection with host:port
107 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000108int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
110 struct sockaddr_in server_addr;
111 struct hostent *server_host;
112
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 WSADATA wsaData;
115
116 if( wsa_init_done == 0 )
117 {
Peter Vaskovic1b08bd92014-05-15 02:54:37 +0200118 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000119 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 wsa_init_done = 1;
122 }
123#else
124 signal( SIGPIPE, SIG_IGN );
125#endif
126
Paul Bakker308a5862014-07-11 11:40:35 +0200127 memset( &server_addr, 0, sizeof( server_addr ) );
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000130 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Paul Bakker4c9301a2013-10-14 16:33:24 +0200132 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000133 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 memcpy( (void *) &server_addr.sin_addr,
136 (void *) server_host->h_addr,
137 server_host->h_length );
138
139 server_addr.sin_family = AF_INET;
140 server_addr.sin_port = net_htons( port );
141
142 if( connect( *fd, (struct sockaddr *) &server_addr,
143 sizeof( server_addr ) ) < 0 )
144 {
145 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000146 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 }
148
149 return( 0 );
150}
151
152/*
153 * Create a listening socket on bind_ip:port
154 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000155int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000156{
157 int n, c[4];
158 struct sockaddr_in server_addr;
159
Paul Bakkerff60ee62010-03-16 21:09:09 +0000160#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 WSADATA wsaData;
162
163 if( wsa_init_done == 0 )
164 {
165 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000166 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 wsa_init_done = 1;
169 }
170#else
171 signal( SIGPIPE, SIG_IGN );
172#endif
173
Paul Bakker308a5862014-07-11 11:40:35 +0200174 memset( &server_addr, 0, sizeof( server_addr ) );
175
Paul Bakker4c9301a2013-10-14 16:33:24 +0200176 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000177 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 n = 1;
Paul Bakker676093e2014-07-08 15:21:28 +0200180 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
181 (const char *) &n, sizeof( n ) ) != 0 )
182 {
183 close( *fd );
184 return( POLARSSL_ERR_NET_SOCKET_FAILED );
185 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakker37286a52013-03-06 16:55:11 +0100187 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 server_addr.sin_family = AF_INET;
189 server_addr.sin_port = net_htons( port );
190
191 if( bind_ip != NULL )
192 {
193 memset( c, 0, sizeof( c ) );
194 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
195
196 for( n = 0; n < 4; n++ )
197 if( c[n] < 0 || c[n] > 255 )
198 break;
199
200 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100201 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000202 ( (uint32_t) c[0] << 24 ) |
203 ( (uint32_t) c[1] << 16 ) |
204 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100205 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 }
207
208 if( bind( *fd, (struct sockaddr *) &server_addr,
209 sizeof( server_addr ) ) < 0 )
210 {
211 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000212 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 }
214
Paul Bakker192381a2011-05-20 12:31:31 +0000215 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 {
217 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000218 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 }
220
221 return( 0 );
222}
223
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200224#if ( defined(_WIN32) || defined(_WIN32_WCE) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225/*
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200226 * Check if the requested operation would be blocking on a non-blocking socket
227 * and thus 'failed' with a negative return value.
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 */
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200229static int net_would_block( int fd )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230{
Paul Bakker22a0ce02014-07-08 11:16:44 +0200231 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200233}
Paul Bakker5121ce52009-01-03 21:22:43 +0000234#else
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200235/*
236 * Check if the requested operation would be blocking on a non-blocking socket
237 * and thus 'failed' with a negative return value.
238 *
239 * Note: on a blocking socket this function always returns 0!
240 */
241static int net_would_block( int fd )
242{
243 /*
244 * Never return 'WOULD BLOCK' on a non-blocking socket
245 */
246 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
247 return( 0 );
248
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 switch( errno )
250 {
251#if defined EAGAIN
252 case EAGAIN:
253#endif
254#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
255 case EWOULDBLOCK:
256#endif
257 return( 1 );
258 }
259 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260}
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263/*
264 * Accept a connection from a remote client
265 */
266int net_accept( int bind_fd, int *client_fd, void *client_ip )
267{
268 struct sockaddr_in client_addr;
269
Paul Bakker394c56f2011-12-20 12:19:03 +0000270#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
271 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 socklen_t n = (socklen_t) sizeof( client_addr );
273#else
274 int n = (int) sizeof( client_addr );
275#endif
276
Paul Bakker4c9301a2013-10-14 16:33:24 +0200277 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
278 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
280 if( *client_fd < 0 )
281 {
Manuel Pégourié-Gonnard3fdfced2014-10-23 15:23:48 +0200282 if( net_would_block( bind_fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000283 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
Paul Bakker40e46942009-01-03 21:51:57 +0000285 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 }
287
288 if( client_ip != NULL )
289 memcpy( client_ip, &client_addr.sin_addr.s_addr,
290 sizeof( client_addr.sin_addr.s_addr ) );
291
292 return( 0 );
293}
294
295/*
296 * Set the socket blocking or non-blocking
297 */
298int net_set_block( int fd )
299{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000300#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000301 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 return( ioctlsocket( fd, FIONBIO, &n ) );
303#else
304 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
305#endif
306}
307
308int net_set_nonblock( int fd )
309{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000310#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000311 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 return( ioctlsocket( fd, FIONBIO, &n ) );
313#else
314 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
315#endif
316}
317
318/*
319 * Portable usleep helper
320 */
321void net_usleep( unsigned long usec )
322{
323 struct timeval tv;
324 tv.tv_sec = 0;
Manuel Pégourié-Gonnard0b0b5222014-10-23 15:17:27 +0200325#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || \
326 (defined(__APPLE__) && defined(__MACH__)))
327 tv.tv_usec = (suseconds_t) usec;
328#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 tv.tv_usec = usec;
Manuel Pégourié-Gonnard0b0b5222014-10-23 15:17:27 +0200330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 select( 0, NULL, NULL, NULL, &tv );
332}
333
334/*
335 * Read at most 'len' characters
336 */
Paul Bakker23986e52011-04-24 08:57:21 +0000337int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200338{
339 int fd = *((int *) ctx);
Manuel Pégourié-Gonnard0b0b5222014-10-23 15:17:27 +0200340 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 if( ret < 0 )
343 {
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200344 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000345 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000349 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350#else
351 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000352 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000355 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356#endif
357
Paul Bakker40e46942009-01-03 21:51:57 +0000358 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 }
360
361 return( ret );
362}
363
364/*
365 * Write at most 'len' characters
366 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000367int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000368{
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200369 int fd = *((int *) ctx);
Manuel Pégourié-Gonnard0b0b5222014-10-23 15:17:27 +0200370 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372 if( ret < 0 )
373 {
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200374 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000375 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000379 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380#else
381 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000382 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
384 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000385 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386#endif
387
Paul Bakker40e46942009-01-03 21:51:57 +0000388 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 }
390
391 return( ret );
392}
393
394/*
395 * Gracefully close the connection
396 */
397void net_close( int fd )
398{
399 shutdown( fd, 2 );
400 close( fd );
401}
402
403#endif