blob: c4ad42416bf2995650b4c9bffd2b6727bf9416aa [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/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100118 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100120static void net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000121{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100122#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
123 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 WSADATA wsaData;
125
126 if( wsa_init_done == 0 )
127 {
128 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000129 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131 wsa_init_done = 1;
132 }
133#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100134#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 signal( SIGPIPE, SIG_IGN );
136#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100137#endif
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100138}
139
140/*
141 * Initiate a TCP connection with host:port
142 */
143int net_connect( int *fd, const char *host, int port )
144{
145 struct sockaddr_in server_addr;
146 struct hostent *server_host;
147
148 net_prepare();
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000151 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakkerbbc10072013-10-14 16:33:24 +0200153 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000154 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156 memcpy( (void *) &server_addr.sin_addr,
157 (void *) server_host->h_addr,
158 server_host->h_length );
159
160 server_addr.sin_family = AF_INET;
161 server_addr.sin_port = net_htons( port );
162
163 if( connect( *fd, (struct sockaddr *) &server_addr,
164 sizeof( server_addr ) ) < 0 )
165 {
166 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000167 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 }
169
170 return( 0 );
171}
172
173/*
174 * Create a listening socket on bind_ip:port
175 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000176int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000177{
178 int n, c[4];
179 struct sockaddr_in server_addr;
180
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100181 net_prepare();
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
Paul Bakkerbbc10072013-10-14 16:33:24 +0200183 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000184 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 n = 1;
187 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
188 (const char *) &n, sizeof( n ) );
189
Paul Bakker37286a52013-03-06 16:55:11 +0100190 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 server_addr.sin_family = AF_INET;
192 server_addr.sin_port = net_htons( port );
193
194 if( bind_ip != NULL )
195 {
196 memset( c, 0, sizeof( c ) );
197 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
198
199 for( n = 0; n < 4; n++ )
200 if( c[n] < 0 || c[n] > 255 )
201 break;
202
203 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100204 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000205 ( (uint32_t) c[0] << 24 ) |
206 ( (uint32_t) c[1] << 16 ) |
207 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100208 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 }
210
211 if( bind( *fd, (struct sockaddr *) &server_addr,
212 sizeof( server_addr ) ) < 0 )
213 {
214 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000215 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 }
217
Paul Bakker192381a2011-05-20 12:31:31 +0000218 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 {
220 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000221 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 }
223
224 return( 0 );
225}
226
227/*
228 * Check if the current operation is blocking
229 */
230static int net_is_blocking( void )
231{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100232#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
233 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 return( WSAGetLastError() == WSAEWOULDBLOCK );
235#else
236 switch( errno )
237 {
238#if defined EAGAIN
239 case EAGAIN:
240#endif
241#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
242 case EWOULDBLOCK:
243#endif
244 return( 1 );
245 }
246 return( 0 );
247#endif
248}
249
250/*
251 * Accept a connection from a remote client
252 */
253int net_accept( int bind_fd, int *client_fd, void *client_ip )
254{
255 struct sockaddr_in client_addr;
256
Paul Bakker394c56f2011-12-20 12:19:03 +0000257#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
258 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 socklen_t n = (socklen_t) sizeof( client_addr );
260#else
261 int n = (int) sizeof( client_addr );
262#endif
263
Paul Bakkerbbc10072013-10-14 16:33:24 +0200264 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
265 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 if( *client_fd < 0 )
268 {
269 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000270 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Paul Bakker40e46942009-01-03 21:51:57 +0000272 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 }
274
275 if( client_ip != NULL )
276 memcpy( client_ip, &client_addr.sin_addr.s_addr,
277 sizeof( client_addr.sin_addr.s_addr ) );
278
279 return( 0 );
280}
281
282/*
283 * Set the socket blocking or non-blocking
284 */
285int net_set_block( int fd )
286{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100287#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
288 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000289 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 return( ioctlsocket( fd, FIONBIO, &n ) );
291#else
292 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
293#endif
294}
295
296int net_set_nonblock( int fd )
297{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100298#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
299 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000300 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 return( ioctlsocket( fd, FIONBIO, &n ) );
302#else
303 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
304#endif
305}
306
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200307#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000308/*
309 * Portable usleep helper
310 */
311void net_usleep( unsigned long usec )
312{
313 struct timeval tv;
314 tv.tv_sec = 0;
315 tv.tv_usec = usec;
316 select( 0, NULL, NULL, NULL, &tv );
317}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200318#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320/*
321 * Read at most 'len' characters
322 */
Paul Bakker23986e52011-04-24 08:57:21 +0000323int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100324{
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 int ret = read( *((int *) ctx), buf, len );
326
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 if( ret < 0 )
328 {
329 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000330 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100332#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
333 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000335 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336#else
337 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000338 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000341 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342#endif
343
Paul Bakker40e46942009-01-03 21:51:57 +0000344 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 }
346
347 return( ret );
348}
349
350/*
351 * Write at most 'len' characters
352 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000353int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000354{
355 int ret = write( *((int *) ctx), buf, len );
356
357 if( ret < 0 )
358 {
359 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000360 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100362#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
363 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000365 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366#else
367 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000368 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000371 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000372#endif
373
Paul Bakker40e46942009-01-03 21:51:57 +0000374 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 }
376
377 return( ret );
378}
379
380/*
381 * Gracefully close the connection
382 */
383void net_close( int fd )
384{
385 shutdown( fd, 2 );
386 close( fd );
387}
388
389#endif