blob: fedde16fef0be2be33396fd785d8dbb3a6fd525a [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 Bakker1d4f30c2009-04-19 18:55:16 +000093#else
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000094#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakker1d4f30c2009-04-19 18:55:16 +000097unsigned short net_htons(unsigned short n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000098#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100/*
101 * Initiate a TCP connection with host:port
102 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000103int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
105 struct sockaddr_in server_addr;
106 struct hostent *server_host;
107
Paul Bakkerff60ee62010-03-16 21:09:09 +0000108#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 WSADATA wsaData;
110
111 if( wsa_init_done == 0 )
112 {
113 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000114 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 wsa_init_done = 1;
117 }
118#else
119 signal( SIGPIPE, SIG_IGN );
120#endif
121
122 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000123 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000126 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128 memcpy( (void *) &server_addr.sin_addr,
129 (void *) server_host->h_addr,
130 server_host->h_length );
131
132 server_addr.sin_family = AF_INET;
133 server_addr.sin_port = net_htons( port );
134
135 if( connect( *fd, (struct sockaddr *) &server_addr,
136 sizeof( server_addr ) ) < 0 )
137 {
138 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000139 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 }
141
142 return( 0 );
143}
144
145/*
146 * Create a listening socket on bind_ip:port
147 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000148int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149{
150 int n, c[4];
151 struct sockaddr_in server_addr;
152
Paul Bakkerff60ee62010-03-16 21:09:09 +0000153#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 WSADATA wsaData;
155
156 if( wsa_init_done == 0 )
157 {
158 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000159 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161 wsa_init_done = 1;
162 }
163#else
164 signal( SIGPIPE, SIG_IGN );
165#endif
166
167 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000168 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170 n = 1;
171 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
172 (const char *) &n, sizeof( n ) );
173
174 server_addr.sin_addr.s_addr = INADDR_ANY;
175 server_addr.sin_family = AF_INET;
176 server_addr.sin_port = net_htons( port );
177
178 if( bind_ip != NULL )
179 {
180 memset( c, 0, sizeof( c ) );
181 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
182
183 for( n = 0; n < 4; n++ )
184 if( c[n] < 0 || c[n] > 255 )
185 break;
186
187 if( n == 4 )
188 server_addr.sin_addr.s_addr =
Paul Bakker5c2364c2012-10-01 14:41:15 +0000189 ( (uint32_t) c[0] << 24 ) |
190 ( (uint32_t) c[1] << 16 ) |
191 ( (uint32_t) c[2] << 8 ) |
192 ( (uint32_t) c[3] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 }
194
195 if( bind( *fd, (struct sockaddr *) &server_addr,
196 sizeof( server_addr ) ) < 0 )
197 {
198 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000199 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 }
201
Paul Bakker192381a2011-05-20 12:31:31 +0000202 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 {
204 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000205 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 }
207
208 return( 0 );
209}
210
211/*
212 * Check if the current operation is blocking
213 */
214static int net_is_blocking( void )
215{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000216#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 return( WSAGetLastError() == WSAEWOULDBLOCK );
218#else
219 switch( errno )
220 {
221#if defined EAGAIN
222 case EAGAIN:
223#endif
224#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
225 case EWOULDBLOCK:
226#endif
227 return( 1 );
228 }
229 return( 0 );
230#endif
231}
232
233/*
234 * Accept a connection from a remote client
235 */
236int net_accept( int bind_fd, int *client_fd, void *client_ip )
237{
238 struct sockaddr_in client_addr;
239
Paul Bakker394c56f2011-12-20 12:19:03 +0000240#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
241 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 socklen_t n = (socklen_t) sizeof( client_addr );
243#else
244 int n = (int) sizeof( client_addr );
245#endif
246
247 *client_fd = accept( bind_fd, (struct sockaddr *)
248 &client_addr, &n );
249
250 if( *client_fd < 0 )
251 {
252 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000253 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Paul Bakker40e46942009-01-03 21:51:57 +0000255 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 }
257
258 if( client_ip != NULL )
259 memcpy( client_ip, &client_addr.sin_addr.s_addr,
260 sizeof( client_addr.sin_addr.s_addr ) );
261
262 return( 0 );
263}
264
265/*
266 * Set the socket blocking or non-blocking
267 */
268int net_set_block( int fd )
269{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000270#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000271 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 return( ioctlsocket( fd, FIONBIO, &n ) );
273#else
274 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
275#endif
276}
277
278int net_set_nonblock( int fd )
279{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000280#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000281 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 return( ioctlsocket( fd, FIONBIO, &n ) );
283#else
284 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
285#endif
286}
287
288/*
289 * Portable usleep helper
290 */
291void net_usleep( unsigned long usec )
292{
293 struct timeval tv;
294 tv.tv_sec = 0;
295 tv.tv_usec = usec;
296 select( 0, NULL, NULL, NULL, &tv );
297}
298
299/*
300 * Read at most 'len' characters
301 */
Paul Bakker23986e52011-04-24 08:57:21 +0000302int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000303{
304 int ret = read( *((int *) ctx), buf, len );
305
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 if( ret < 0 )
307 {
308 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000309 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
Paul Bakkerff60ee62010-03-16 21:09:09 +0000311#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000313 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314#else
315 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000316 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000319 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320#endif
321
Paul Bakker40e46942009-01-03 21:51:57 +0000322 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 }
324
325 return( ret );
326}
327
328/*
329 * Write at most 'len' characters
330 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000331int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000332{
333 int ret = write( *((int *) ctx), buf, len );
334
335 if( ret < 0 )
336 {
337 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000338 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Paul Bakkerff60ee62010-03-16 21:09:09 +0000340#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000342 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343#else
344 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000345 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000348 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349#endif
350
Paul Bakker40e46942009-01-03 21:51:57 +0000351 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 }
353
354 return( ret );
355}
356
357/*
358 * Gracefully close the connection
359 */
360void net_close( int fd )
361{
362 shutdown( fd, 2 );
363 close( fd );
364}
365
366#endif