blob: 6333b0ff0f6bd4f6d65c3e8c0bee84ad77ae16ac [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * TCP networking functions
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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>
55#include <sys/time.h>
56#include <unistd.h>
57#include <signal.h>
58#include <fcntl.h>
59#include <netdb.h>
60#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000061
Paul Bakker6a2f8572012-08-23 07:45:37 +000062#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
63 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000064#include <sys/endian.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000065#elif defined(__APPLE__)
66#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000067#elif defined(sun)
68#include <sys/isa_defs.h>
Paul Bakker854963c2009-07-19 20:50:11 +000069#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000070#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
73#endif
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075#include <stdlib.h>
76#include <stdio.h>
77#include <time.h>
78
Paul Bakker5c2364c2012-10-01 14:41:15 +000079#ifdef _MSC_VER
80#include <basetsd.h>
81typedef UINT32 uint32_t;
82#else
83#include <inttypes.h>
84#endif
85
Paul Bakker5121ce52009-01-03 21:22:43 +000086/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000087 * htons() is not always available.
88 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
89 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000091#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000092#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +010093#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000094#else
Paul Bakker37286a52013-03-06 16:55:11 +010095#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
96 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
97#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
98 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
99 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
100 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000101#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000103unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100104unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000105#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100106#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/*
109 * Initiate a TCP connection with host:port
110 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000112{
113 struct sockaddr_in server_addr;
114 struct hostent *server_host;
115
Paul Bakkerff60ee62010-03-16 21:09:09 +0000116#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 WSADATA wsaData;
118
119 if( wsa_init_done == 0 )
120 {
121 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000122 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 wsa_init_done = 1;
125 }
126#else
127 signal( SIGPIPE, SIG_IGN );
128#endif
129
130 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000131 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakker4c9301a2013-10-14 16:33:24 +0200133 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000134 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136 memcpy( (void *) &server_addr.sin_addr,
137 (void *) server_host->h_addr,
138 server_host->h_length );
139
140 server_addr.sin_family = AF_INET;
141 server_addr.sin_port = net_htons( port );
142
143 if( connect( *fd, (struct sockaddr *) &server_addr,
144 sizeof( server_addr ) ) < 0 )
145 {
146 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000147 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 }
149
150 return( 0 );
151}
152
153/*
154 * Create a listening socket on bind_ip:port
155 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000156int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157{
158 int n, c[4];
159 struct sockaddr_in server_addr;
160
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 WSADATA wsaData;
163
164 if( wsa_init_done == 0 )
165 {
166 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000167 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 wsa_init_done = 1;
170 }
171#else
172 signal( SIGPIPE, SIG_IGN );
173#endif
174
Paul Bakker4c9301a2013-10-14 16:33:24 +0200175 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000176 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178 n = 1;
179 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
180 (const char *) &n, sizeof( n ) );
181
Paul Bakker37286a52013-03-06 16:55:11 +0100182 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 server_addr.sin_family = AF_INET;
184 server_addr.sin_port = net_htons( port );
185
186 if( bind_ip != NULL )
187 {
188 memset( c, 0, sizeof( c ) );
189 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
190
191 for( n = 0; n < 4; n++ )
192 if( c[n] < 0 || c[n] > 255 )
193 break;
194
195 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100196 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000197 ( (uint32_t) c[0] << 24 ) |
198 ( (uint32_t) c[1] << 16 ) |
199 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100200 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 }
202
203 if( bind( *fd, (struct sockaddr *) &server_addr,
204 sizeof( server_addr ) ) < 0 )
205 {
206 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000207 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 }
209
Paul Bakker192381a2011-05-20 12:31:31 +0000210 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 {
212 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000213 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 }
215
216 return( 0 );
217}
218
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200219#if ( defined(_WIN32) || defined(_WIN32_WCE) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220/*
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200221 * Check if the requested operation would be blocking on a non-blocking socket
222 * and thus 'failed' with a negative return value.
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200224static int net_would_block( int fd )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225{
Paul Bakker22a0ce02014-07-08 11:16:44 +0200226 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200228}
Paul Bakker5121ce52009-01-03 21:22:43 +0000229#else
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200230/*
231 * Check if the requested operation would be blocking on a non-blocking socket
232 * and thus 'failed' with a negative return value.
233 *
234 * Note: on a blocking socket this function always returns 0!
235 */
236static int net_would_block( int fd )
237{
238 /*
239 * Never return 'WOULD BLOCK' on a non-blocking socket
240 */
241 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
242 return( 0 );
243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 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 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255}
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
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 Bakker4c9301a2013-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 {
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200277 if( net_would_block( *client_fd ) != 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 Bakkerff60ee62010-03-16 21:09:09 +0000295#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000296 u_long n = 0;
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
303int net_set_nonblock( int fd )
304{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000305#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000306 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 return( ioctlsocket( fd, FIONBIO, &n ) );
308#else
309 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
310#endif
311}
312
313/*
314 * Portable usleep helper
315 */
316void net_usleep( unsigned long usec )
317{
318 struct timeval tv;
319 tv.tv_sec = 0;
320 tv.tv_usec = usec;
321 select( 0, NULL, NULL, NULL, &tv );
322}
323
324/*
325 * Read at most 'len' characters
326 */
Paul Bakker23986e52011-04-24 08:57:21 +0000327int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200328{
329 int fd = *((int *) ctx);
330 int ret = read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 if( ret < 0 )
333 {
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200334 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000335 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Paul Bakkerff60ee62010-03-16 21:09:09 +0000337#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000339 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340#else
341 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000342 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000345 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346#endif
347
Paul Bakker40e46942009-01-03 21:51:57 +0000348 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 }
350
351 return( ret );
352}
353
354/*
355 * Write at most 'len' characters
356 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000357int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358{
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200359 int fd = *((int *) ctx);
360 int ret = write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362 if( ret < 0 )
363 {
Paul Bakker1e7c3d22014-07-07 16:41:31 +0200364 if( net_would_block( fd ) != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000365 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
Paul Bakkerff60ee62010-03-16 21:09:09 +0000367#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000369 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370#else
371 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000372 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000375 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376#endif
377
Paul Bakker40e46942009-01-03 21:51:57 +0000378 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 }
380
381 return( ret );
382}
383
384/*
385 * Gracefully close the connection
386 */
387void net_close( int fd )
388{
389 shutdown( fd, 2 );
390 close( fd );
391}
392
393#endif