blob: 7059fd29f69cd154483ad07edc7556c2f4c418a1 [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 Bakker1fad5bf2011-07-01 09:07:24 +000062#if defined(__FreeBSD__) || defined(__OpenBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000063#include <sys/endian.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000064#elif defined(__APPLE__)
65#include <machine/endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000066#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000067#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70#endif
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072#include <stdlib.h>
73#include <stdio.h>
74#include <time.h>
75
76/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000077 * htons() is not always available.
78 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
79 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000081#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000082#define POLARSSL_HTONS(n) (n)
Paul Bakkerb5f27272013-03-11 16:53:25 +010083#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000084#else
Paul Bakkerb5f27272013-03-11 16:53:25 +010085#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
86 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
87#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
88 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
89 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
90 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakker1d4f30c2009-04-19 18:55:16 +000093unsigned short net_htons(unsigned short n);
Paul Bakkerb5f27272013-03-11 16:53:25 +010094unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000095#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakkerb5f27272013-03-11 16:53:25 +010096#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98/*
99 * Initiate a TCP connection with host:port
100 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
103 struct sockaddr_in server_addr;
104 struct hostent *server_host;
105
Paul Bakkerff60ee62010-03-16 21:09:09 +0000106#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 WSADATA wsaData;
108
109 if( wsa_init_done == 0 )
110 {
111 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000112 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 wsa_init_done = 1;
115 }
116#else
117 signal( SIGPIPE, SIG_IGN );
118#endif
119
120 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000121 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000124 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126 memcpy( (void *) &server_addr.sin_addr,
127 (void *) server_host->h_addr,
128 server_host->h_length );
129
130 server_addr.sin_family = AF_INET;
131 server_addr.sin_port = net_htons( port );
132
133 if( connect( *fd, (struct sockaddr *) &server_addr,
134 sizeof( server_addr ) ) < 0 )
135 {
136 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000137 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 }
139
140 return( 0 );
141}
142
143/*
144 * Create a listening socket on bind_ip:port
145 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147{
148 int n, c[4];
149 struct sockaddr_in server_addr;
150
Paul Bakkerff60ee62010-03-16 21:09:09 +0000151#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 WSADATA wsaData;
153
154 if( wsa_init_done == 0 )
155 {
156 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000157 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 wsa_init_done = 1;
160 }
161#else
162 signal( SIGPIPE, SIG_IGN );
163#endif
164
165 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000166 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 n = 1;
169 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
170 (const char *) &n, sizeof( n ) );
171
Paul Bakkerb5f27272013-03-11 16:53:25 +0100172 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 server_addr.sin_family = AF_INET;
174 server_addr.sin_port = net_htons( port );
175
176 if( bind_ip != NULL )
177 {
178 memset( c, 0, sizeof( c ) );
179 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
180
181 for( n = 0; n < 4; n++ )
182 if( c[n] < 0 || c[n] > 255 )
183 break;
184
185 if( n == 4 )
Paul Bakkerb5f27272013-03-11 16:53:25 +0100186 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 ( (unsigned long) c[0] << 24 ) |
188 ( (unsigned long) c[1] << 16 ) |
189 ( (unsigned long) c[2] << 8 ) |
Paul Bakkerb5f27272013-03-11 16:53:25 +0100190 ( (unsigned long) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 }
192
193 if( bind( *fd, (struct sockaddr *) &server_addr,
194 sizeof( server_addr ) ) < 0 )
195 {
196 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000197 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 }
199
Paul Bakker192381a2011-05-20 12:31:31 +0000200 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 {
202 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000203 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 }
205
206 return( 0 );
207}
208
209/*
210 * Check if the current operation is blocking
211 */
212static int net_is_blocking( void )
213{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000214#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 return( WSAGetLastError() == WSAEWOULDBLOCK );
216#else
217 switch( errno )
218 {
219#if defined EAGAIN
220 case EAGAIN:
221#endif
222#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
223 case EWOULDBLOCK:
224#endif
225 return( 1 );
226 }
227 return( 0 );
228#endif
229}
230
231/*
232 * Accept a connection from a remote client
233 */
234int net_accept( int bind_fd, int *client_fd, void *client_ip )
235{
236 struct sockaddr_in client_addr;
237
Paul Bakkerd567aa22011-12-22 10:06:27 +0000238#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
239 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 socklen_t n = (socklen_t) sizeof( client_addr );
241#else
242 int n = (int) sizeof( client_addr );
243#endif
244
245 *client_fd = accept( bind_fd, (struct sockaddr *)
246 &client_addr, &n );
247
248 if( *client_fd < 0 )
249 {
250 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000251 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
Paul Bakker40e46942009-01-03 21:51:57 +0000253 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 }
255
256 if( client_ip != NULL )
257 memcpy( client_ip, &client_addr.sin_addr.s_addr,
258 sizeof( client_addr.sin_addr.s_addr ) );
259
260 return( 0 );
261}
262
263/*
264 * Set the socket blocking or non-blocking
265 */
266int net_set_block( int fd )
267{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000268#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000269 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 return( ioctlsocket( fd, FIONBIO, &n ) );
271#else
272 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
273#endif
274}
275
276int net_set_nonblock( int fd )
277{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000278#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000279 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 return( ioctlsocket( fd, FIONBIO, &n ) );
281#else
282 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
283#endif
284}
285
286/*
287 * Portable usleep helper
288 */
289void net_usleep( unsigned long usec )
290{
291 struct timeval tv;
292 tv.tv_sec = 0;
293 tv.tv_usec = usec;
294 select( 0, NULL, NULL, NULL, &tv );
295}
296
297/*
298 * Read at most 'len' characters
299 */
Paul Bakker23986e52011-04-24 08:57:21 +0000300int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000301{
302 int ret = read( *((int *) ctx), buf, len );
303
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 if( ret < 0 )
305 {
306 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000307 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
Paul Bakkerff60ee62010-03-16 21:09:09 +0000309#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000311 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312#else
313 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000314 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000317 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318#endif
319
Paul Bakker40e46942009-01-03 21:51:57 +0000320 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 }
322
323 return( ret );
324}
325
326/*
327 * Write at most 'len' characters
328 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000329int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330{
331 int ret = write( *((int *) ctx), buf, len );
332
333 if( ret < 0 )
334 {
335 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000336 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000340 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341#else
342 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000346 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347#endif
348
Paul Bakker40e46942009-01-03 21:51:57 +0000349 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 }
351
352 return( ret );
353}
354
355/*
356 * Gracefully close the connection
357 */
358void net_close( int fd )
359{
360 shutdown( fd, 2 );
361 close( fd );
362}
363
364#endif