blob: 2b21c57f1655ad8a34795d80e1c49e023d4b92a7 [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 Bakkerff60ee62010-03-16 21:09:09 +000032#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34#include <winsock2.h>
35#include <windows.h>
36
37#if defined(_WIN32_WCE)
38#pragma comment( lib, "ws2.lib" )
39#else
40#pragma comment( lib, "ws2_32.lib" )
41#endif
42
Paul Bakkerf4f69682011-04-24 16:08:12 +000043#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
44#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000045#define close(fd) closesocket(fd)
46
47static int wsa_init_done = 0;
48
49#else
50
51#include <sys/types.h>
52#include <sys/socket.h>
53#include <netinet/in.h>
54#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020055#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000056#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000058#include <unistd.h>
59#include <signal.h>
60#include <fcntl.h>
61#include <netdb.h>
62#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000063
Paul Bakker6a2f8572012-08-23 07:45:37 +000064#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
65 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000066#include <sys/endian.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020067#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000068#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000069#elif defined(sun)
70#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020071#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
72#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000073#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000074#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000075#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77#endif
78
Paul Bakker5121ce52009-01-03 21:22:43 +000079#include <stdlib.h>
80#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020081
82#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000083#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Paul Bakker5c2364c2012-10-01 14:41:15 +000086#ifdef _MSC_VER
87#include <basetsd.h>
88typedef UINT32 uint32_t;
89#else
90#include <inttypes.h>
91#endif
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000094 * htons() is not always available.
95 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
96 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000098#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000099#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100100#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000101#else
Paul Bakker37286a52013-03-06 16:55:11 +0100102#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
103 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
104#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
105 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
106 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
107 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000108#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000110unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100111unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000112#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100113#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/*
116 * Initiate a TCP connection with host:port
117 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000118int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000119{
120 struct sockaddr_in server_addr;
121 struct hostent *server_host;
122
Paul Bakkerff60ee62010-03-16 21:09:09 +0000123#if defined(_WIN32) || defined(_WIN32_WCE)
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
134 signal( SIGPIPE, SIG_IGN );
135#endif
136
137 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000138 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000141 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143 memcpy( (void *) &server_addr.sin_addr,
144 (void *) server_host->h_addr,
145 server_host->h_length );
146
147 server_addr.sin_family = AF_INET;
148 server_addr.sin_port = net_htons( port );
149
150 if( connect( *fd, (struct sockaddr *) &server_addr,
151 sizeof( server_addr ) ) < 0 )
152 {
153 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000154 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 }
156
157 return( 0 );
158}
159
160/*
161 * Create a listening socket on bind_ip:port
162 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000163int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000164{
165 int n, c[4];
166 struct sockaddr_in server_addr;
167
Paul Bakkerff60ee62010-03-16 21:09:09 +0000168#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 WSADATA wsaData;
170
171 if( wsa_init_done == 0 )
172 {
173 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000174 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176 wsa_init_done = 1;
177 }
178#else
179 signal( SIGPIPE, SIG_IGN );
180#endif
181
182 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000183 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 n = 1;
186 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
187 (const char *) &n, sizeof( n ) );
188
Paul Bakker37286a52013-03-06 16:55:11 +0100189 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 server_addr.sin_family = AF_INET;
191 server_addr.sin_port = net_htons( port );
192
193 if( bind_ip != NULL )
194 {
195 memset( c, 0, sizeof( c ) );
196 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
197
198 for( n = 0; n < 4; n++ )
199 if( c[n] < 0 || c[n] > 255 )
200 break;
201
202 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100203 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000204 ( (uint32_t) c[0] << 24 ) |
205 ( (uint32_t) c[1] << 16 ) |
206 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100207 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 }
209
210 if( bind( *fd, (struct sockaddr *) &server_addr,
211 sizeof( server_addr ) ) < 0 )
212 {
213 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000214 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 }
216
Paul Bakker192381a2011-05-20 12:31:31 +0000217 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 {
219 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000220 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 }
222
223 return( 0 );
224}
225
226/*
227 * Check if the current operation is blocking
228 */
229static int net_is_blocking( void )
230{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000231#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 return( WSAGetLastError() == WSAEWOULDBLOCK );
233#else
234 switch( errno )
235 {
236#if defined EAGAIN
237 case EAGAIN:
238#endif
239#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
240 case EWOULDBLOCK:
241#endif
242 return( 1 );
243 }
244 return( 0 );
245#endif
246}
247
248/*
249 * Accept a connection from a remote client
250 */
251int net_accept( int bind_fd, int *client_fd, void *client_ip )
252{
253 struct sockaddr_in client_addr;
254
Paul Bakker394c56f2011-12-20 12:19:03 +0000255#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
256 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 socklen_t n = (socklen_t) sizeof( client_addr );
258#else
259 int n = (int) sizeof( client_addr );
260#endif
261
262 *client_fd = accept( bind_fd, (struct sockaddr *)
263 &client_addr, &n );
264
265 if( *client_fd < 0 )
266 {
267 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000268 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Paul Bakker40e46942009-01-03 21:51:57 +0000270 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 }
272
273 if( client_ip != NULL )
274 memcpy( client_ip, &client_addr.sin_addr.s_addr,
275 sizeof( client_addr.sin_addr.s_addr ) );
276
277 return( 0 );
278}
279
280/*
281 * Set the socket blocking or non-blocking
282 */
283int net_set_block( int fd )
284{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000285#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000286 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 return( ioctlsocket( fd, FIONBIO, &n ) );
288#else
289 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
290#endif
291}
292
293int net_set_nonblock( int fd )
294{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000295#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000296 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 return( ioctlsocket( fd, FIONBIO, &n ) );
298#else
299 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
300#endif
301}
302
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200303#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000304/*
305 * Portable usleep helper
306 */
307void net_usleep( unsigned long usec )
308{
309 struct timeval tv;
310 tv.tv_sec = 0;
311 tv.tv_usec = usec;
312 select( 0, NULL, NULL, NULL, &tv );
313}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200314#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316/*
317 * Read at most 'len' characters
318 */
Paul Bakker23986e52011-04-24 08:57:21 +0000319int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320{
321 int ret = read( *((int *) ctx), buf, len );
322
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 if( ret < 0 )
324 {
325 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000326 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Paul Bakkerff60ee62010-03-16 21:09:09 +0000328#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000330 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331#else
332 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000333 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000336 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337#endif
338
Paul Bakker40e46942009-01-03 21:51:57 +0000339 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 }
341
342 return( ret );
343}
344
345/*
346 * Write at most 'len' characters
347 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000348int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349{
350 int ret = write( *((int *) ctx), buf, len );
351
352 if( ret < 0 )
353 {
354 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000355 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Paul Bakkerff60ee62010-03-16 21:09:09 +0000357#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000359 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360#else
361 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000362 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000365 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366#endif
367
Paul Bakker40e46942009-01-03 21:51:57 +0000368 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 }
370
371 return( ret );
372}
373
374/*
375 * Gracefully close the connection
376 */
377void net_close( int fd )
378{
379 shutdown( fd, 2 );
380 close( fd );
381}
382
383#endif