blob: be2785d98da041a9e0e8eca51c75971101e7a733 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakkerfa9b1002013-07-03 15:31:03 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerfa6a6202013-10-28 18:48:30 +010032#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
33 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35#include <winsock2.h>
36#include <windows.h>
37
38#if defined(_WIN32_WCE)
39#pragma comment( lib, "ws2.lib" )
40#else
41#pragma comment( lib, "ws2_32.lib" )
42#endif
43
Paul Bakkerf4f69682011-04-24 16:08:12 +000044#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
45#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000046#define close(fd) closesocket(fd)
47
48static int wsa_init_done = 0;
49
50#else
51
52#include <sys/types.h>
53#include <sys/socket.h>
54#include <netinet/in.h>
55#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059#include <unistd.h>
60#include <signal.h>
61#include <fcntl.h>
62#include <netdb.h>
63#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000064
Paul Bakker6a2f8572012-08-23 07:45:37 +000065#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
66 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000067#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
69 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000070#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000071#elif defined(sun)
72#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020073#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
74#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000075#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000076#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79#endif
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081#include <stdlib.h>
82#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020083
84#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000085#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020086#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Paul Bakkerfa6a6202013-10-28 18:48:30 +010088#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000089#include <basetsd.h>
90typedef UINT32 uint32_t;
91#else
92#include <inttypes.h>
93#endif
94
Paul Bakker5121ce52009-01-03 21:22:43 +000095/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000096 * htons() is not always available.
97 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +010098 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +000099 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000100#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000101#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100102#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000103#else
Paul Bakker37286a52013-03-06 16:55:11 +0100104#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
105 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
106#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
107 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
108 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
109 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000112unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100113unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000114#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100115#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117/*
118 * Initiate a TCP connection with host:port
119 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000120int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000121{
122 struct sockaddr_in server_addr;
123 struct hostent *server_host;
124
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100125#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
126 !defined(EFI32)
127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 WSADATA wsaData;
129
130 if( wsa_init_done == 0 )
131 {
132 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000133 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 wsa_init_done = 1;
136 }
137#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100138#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 signal( SIGPIPE, SIG_IGN );
140#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100141#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000144 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Paul Bakkerbbc10072013-10-14 16:33:24 +0200146 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000147 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 memcpy( (void *) &server_addr.sin_addr,
150 (void *) server_host->h_addr,
151 server_host->h_length );
152
153 server_addr.sin_family = AF_INET;
154 server_addr.sin_port = net_htons( port );
155
156 if( connect( *fd, (struct sockaddr *) &server_addr,
157 sizeof( server_addr ) ) < 0 )
158 {
159 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000160 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 }
162
163 return( 0 );
164}
165
166/*
167 * Create a listening socket on bind_ip:port
168 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000169int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170{
171 int n, c[4];
172 struct sockaddr_in server_addr;
173
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100174#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
175 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 WSADATA wsaData;
177
178 if( wsa_init_done == 0 )
179 {
180 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000181 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 wsa_init_done = 1;
184 }
185#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100186#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 signal( SIGPIPE, SIG_IGN );
188#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100189#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
Paul Bakkerbbc10072013-10-14 16:33:24 +0200191 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 n = 1;
195 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
196 (const char *) &n, sizeof( n ) );
197
Paul Bakker37286a52013-03-06 16:55:11 +0100198 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 server_addr.sin_family = AF_INET;
200 server_addr.sin_port = net_htons( port );
201
202 if( bind_ip != NULL )
203 {
204 memset( c, 0, sizeof( c ) );
205 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
206
207 for( n = 0; n < 4; n++ )
208 if( c[n] < 0 || c[n] > 255 )
209 break;
210
211 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100212 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000213 ( (uint32_t) c[0] << 24 ) |
214 ( (uint32_t) c[1] << 16 ) |
215 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100216 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 }
218
219 if( bind( *fd, (struct sockaddr *) &server_addr,
220 sizeof( server_addr ) ) < 0 )
221 {
222 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000223 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 }
225
Paul Bakker192381a2011-05-20 12:31:31 +0000226 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 {
228 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000229 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 }
231
232 return( 0 );
233}
234
235/*
236 * Check if the current operation is blocking
237 */
238static int net_is_blocking( void )
239{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100240#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
241 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 return( WSAGetLastError() == WSAEWOULDBLOCK );
243#else
244 switch( errno )
245 {
246#if defined EAGAIN
247 case EAGAIN:
248#endif
249#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
250 case EWOULDBLOCK:
251#endif
252 return( 1 );
253 }
254 return( 0 );
255#endif
256}
257
258/*
259 * Accept a connection from a remote client
260 */
261int net_accept( int bind_fd, int *client_fd, void *client_ip )
262{
263 struct sockaddr_in client_addr;
264
Paul Bakker394c56f2011-12-20 12:19:03 +0000265#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
266 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 socklen_t n = (socklen_t) sizeof( client_addr );
268#else
269 int n = (int) sizeof( client_addr );
270#endif
271
Paul Bakkerbbc10072013-10-14 16:33:24 +0200272 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
273 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 if( *client_fd < 0 )
276 {
277 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000278 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
Paul Bakker40e46942009-01-03 21:51:57 +0000280 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 }
282
283 if( client_ip != NULL )
284 memcpy( client_ip, &client_addr.sin_addr.s_addr,
285 sizeof( client_addr.sin_addr.s_addr ) );
286
287 return( 0 );
288}
289
290/*
291 * Set the socket blocking or non-blocking
292 */
293int net_set_block( int fd )
294{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100295#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
296 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000297 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 return( ioctlsocket( fd, FIONBIO, &n ) );
299#else
300 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
301#endif
302}
303
304int net_set_nonblock( int fd )
305{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100306#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
307 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000308 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 return( ioctlsocket( fd, FIONBIO, &n ) );
310#else
311 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
312#endif
313}
314
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200315#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000316/*
317 * Portable usleep helper
318 */
319void net_usleep( unsigned long usec )
320{
321 struct timeval tv;
322 tv.tv_sec = 0;
323 tv.tv_usec = usec;
324 select( 0, NULL, NULL, NULL, &tv );
325}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200326#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328/*
329 * Read at most 'len' characters
330 */
Paul Bakker23986e52011-04-24 08:57:21 +0000331int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100332{
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 int ret = read( *((int *) ctx), buf, len );
334
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 if( ret < 0 )
336 {
337 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000338 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100340#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
341 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344#else
345 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000346 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000349 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350#endif
351
Paul Bakker40e46942009-01-03 21:51:57 +0000352 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 }
354
355 return( ret );
356}
357
358/*
359 * Write at most 'len' characters
360 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000361int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000362{
363 int ret = write( *((int *) ctx), buf, len );
364
365 if( ret < 0 )
366 {
367 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000368 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100370#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
371 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000373 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374#else
375 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000376 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000379 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380#endif
381
Paul Bakker40e46942009-01-03 21:51:57 +0000382 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 }
384
385 return( ret );
386}
387
388/*
389 * Gracefully close the connection
390 */
391void net_close( int fd )
392{
393 shutdown( fd, 2 );
394 close( fd );
395}
396
397#endif