blob: da7214de31afb5ade4439064f3c0f4137e24a1b5 [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 Bakkerb3bb6c02009-07-27 21:09:47 +000067#elif defined(__APPLE__)
68#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000069#elif defined(sun)
70#include <sys/isa_defs.h>
Paul Bakker854963c2009-07-19 20:50:11 +000071#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000072#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077#include <stdlib.h>
78#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020079
80#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000081#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Paul Bakker5c2364c2012-10-01 14:41:15 +000084#ifdef _MSC_VER
85#include <basetsd.h>
86typedef UINT32 uint32_t;
87#else
88#include <inttypes.h>
89#endif
90
Paul Bakker5121ce52009-01-03 21:22:43 +000091/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000092 * htons() is not always available.
93 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
94 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000096#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000097#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +010098#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000099#else
Paul Bakker37286a52013-03-06 16:55:11 +0100100#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
101 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
102#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
103 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
104 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
105 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000108unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100109unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000110#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100111#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/*
114 * Initiate a TCP connection with host:port
115 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000116int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000117{
118 struct sockaddr_in server_addr;
119 struct hostent *server_host;
120
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 WSADATA wsaData;
123
124 if( wsa_init_done == 0 )
125 {
126 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000127 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129 wsa_init_done = 1;
130 }
131#else
132 signal( SIGPIPE, SIG_IGN );
133#endif
134
135 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000136 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000139 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 memcpy( (void *) &server_addr.sin_addr,
142 (void *) server_host->h_addr,
143 server_host->h_length );
144
145 server_addr.sin_family = AF_INET;
146 server_addr.sin_port = net_htons( port );
147
148 if( connect( *fd, (struct sockaddr *) &server_addr,
149 sizeof( server_addr ) ) < 0 )
150 {
151 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000152 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 }
154
155 return( 0 );
156}
157
158/*
159 * Create a listening socket on bind_ip:port
160 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162{
163 int n, c[4];
164 struct sockaddr_in server_addr;
165
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 WSADATA wsaData;
168
169 if( wsa_init_done == 0 )
170 {
171 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000172 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174 wsa_init_done = 1;
175 }
176#else
177 signal( SIGPIPE, SIG_IGN );
178#endif
179
180 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000181 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 n = 1;
184 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
185 (const char *) &n, sizeof( n ) );
186
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
224/*
225 * Check if the current operation is blocking
226 */
227static int net_is_blocking( void )
228{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 return( WSAGetLastError() == WSAEWOULDBLOCK );
231#else
232 switch( errno )
233 {
234#if defined EAGAIN
235 case EAGAIN:
236#endif
237#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
238 case EWOULDBLOCK:
239#endif
240 return( 1 );
241 }
242 return( 0 );
243#endif
244}
245
246/*
247 * Accept a connection from a remote client
248 */
249int net_accept( int bind_fd, int *client_fd, void *client_ip )
250{
251 struct sockaddr_in client_addr;
252
Paul Bakker394c56f2011-12-20 12:19:03 +0000253#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
254 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 socklen_t n = (socklen_t) sizeof( client_addr );
256#else
257 int n = (int) sizeof( client_addr );
258#endif
259
260 *client_fd = accept( bind_fd, (struct sockaddr *)
261 &client_addr, &n );
262
263 if( *client_fd < 0 )
264 {
265 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000266 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Paul Bakker40e46942009-01-03 21:51:57 +0000268 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 }
270
271 if( client_ip != NULL )
272 memcpy( client_ip, &client_addr.sin_addr.s_addr,
273 sizeof( client_addr.sin_addr.s_addr ) );
274
275 return( 0 );
276}
277
278/*
279 * Set the socket blocking or non-blocking
280 */
281int net_set_block( int fd )
282{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000284 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 return( ioctlsocket( fd, FIONBIO, &n ) );
286#else
287 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
288#endif
289}
290
291int net_set_nonblock( int fd )
292{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000293#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000294 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 return( ioctlsocket( fd, FIONBIO, &n ) );
296#else
297 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
298#endif
299}
300
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200301#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000302/*
303 * Portable usleep helper
304 */
305void net_usleep( unsigned long usec )
306{
307 struct timeval tv;
308 tv.tv_sec = 0;
309 tv.tv_usec = usec;
310 select( 0, NULL, NULL, NULL, &tv );
311}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200312#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314/*
315 * Read at most 'len' characters
316 */
Paul Bakker23986e52011-04-24 08:57:21 +0000317int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318{
319 int ret = read( *((int *) ctx), buf, len );
320
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 if( ret < 0 )
322 {
323 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000324 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Paul Bakkerff60ee62010-03-16 21:09:09 +0000326#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000328 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329#else
330 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000331 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000334 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335#endif
336
Paul Bakker40e46942009-01-03 21:51:57 +0000337 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 }
339
340 return( ret );
341}
342
343/*
344 * Write at most 'len' characters
345 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000346int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347{
348 int ret = write( *((int *) ctx), buf, len );
349
350 if( ret < 0 )
351 {
352 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000353 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Paul Bakkerff60ee62010-03-16 21:09:09 +0000355#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000357 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358#else
359 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000360 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000363 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364#endif
365
Paul Bakker40e46942009-01-03 21:51:57 +0000366 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 }
368
369 return( ret );
370}
371
372/*
373 * Gracefully close the connection
374 */
375void net_close( int fd )
376{
377 shutdown( fd, 2 );
378 close( fd );
379}
380
381#endif