blob: a0c141ad18ed82bcbe7ffa3c0d6a8dde3830d097 [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
79/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +000080 * htons() is not always available.
81 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
82 * to help determine endianess.
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +000084#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000085#define POLARSSL_HTONS(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +000086#else
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000087#define POLARSSL_HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
Paul Bakker1d4f30c2009-04-19 18:55:16 +000088#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Paul Bakker1d4f30c2009-04-19 18:55:16 +000090unsigned short net_htons(unsigned short n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000091#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/*
94 * Initiate a TCP connection with host:port
95 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000096int net_connect( int *fd, const char *host, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
98 struct sockaddr_in server_addr;
99 struct hostent *server_host;
100
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 WSADATA wsaData;
103
104 if( wsa_init_done == 0 )
105 {
106 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000107 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 wsa_init_done = 1;
110 }
111#else
112 signal( SIGPIPE, SIG_IGN );
113#endif
114
115 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000116 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000119 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 memcpy( (void *) &server_addr.sin_addr,
122 (void *) server_host->h_addr,
123 server_host->h_length );
124
125 server_addr.sin_family = AF_INET;
126 server_addr.sin_port = net_htons( port );
127
128 if( connect( *fd, (struct sockaddr *) &server_addr,
129 sizeof( server_addr ) ) < 0 )
130 {
131 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000132 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
135 return( 0 );
136}
137
138/*
139 * Create a listening socket on bind_ip:port
140 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142{
143 int n, c[4];
144 struct sockaddr_in server_addr;
145
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 WSADATA wsaData;
148
149 if( wsa_init_done == 0 )
150 {
151 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000152 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154 wsa_init_done = 1;
155 }
156#else
157 signal( SIGPIPE, SIG_IGN );
158#endif
159
160 if( ( *fd = socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000161 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 n = 1;
164 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
165 (const char *) &n, sizeof( n ) );
166
167 server_addr.sin_addr.s_addr = INADDR_ANY;
168 server_addr.sin_family = AF_INET;
169 server_addr.sin_port = net_htons( port );
170
171 if( bind_ip != NULL )
172 {
173 memset( c, 0, sizeof( c ) );
174 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
175
176 for( n = 0; n < 4; n++ )
177 if( c[n] < 0 || c[n] > 255 )
178 break;
179
180 if( n == 4 )
181 server_addr.sin_addr.s_addr =
182 ( (unsigned long) c[0] << 24 ) |
183 ( (unsigned long) c[1] << 16 ) |
184 ( (unsigned long) c[2] << 8 ) |
185 ( (unsigned long) c[3] );
186 }
187
188 if( bind( *fd, (struct sockaddr *) &server_addr,
189 sizeof( server_addr ) ) < 0 )
190 {
191 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 }
194
Paul Bakker192381a2011-05-20 12:31:31 +0000195 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 {
197 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000198 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 }
200
201 return( 0 );
202}
203
204/*
205 * Check if the current operation is blocking
206 */
207static int net_is_blocking( void )
208{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000209#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 return( WSAGetLastError() == WSAEWOULDBLOCK );
211#else
212 switch( errno )
213 {
214#if defined EAGAIN
215 case EAGAIN:
216#endif
217#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
218 case EWOULDBLOCK:
219#endif
220 return( 1 );
221 }
222 return( 0 );
223#endif
224}
225
226/*
227 * Accept a connection from a remote client
228 */
229int net_accept( int bind_fd, int *client_fd, void *client_ip )
230{
231 struct sockaddr_in client_addr;
232
Paul Bakker394c56f2011-12-20 12:19:03 +0000233#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
234 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 socklen_t n = (socklen_t) sizeof( client_addr );
236#else
237 int n = (int) sizeof( client_addr );
238#endif
239
240 *client_fd = accept( bind_fd, (struct sockaddr *)
241 &client_addr, &n );
242
243 if( *client_fd < 0 )
244 {
245 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000246 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Paul Bakker40e46942009-01-03 21:51:57 +0000248 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 }
250
251 if( client_ip != NULL )
252 memcpy( client_ip, &client_addr.sin_addr.s_addr,
253 sizeof( client_addr.sin_addr.s_addr ) );
254
255 return( 0 );
256}
257
258/*
259 * Set the socket blocking or non-blocking
260 */
261int net_set_block( int fd )
262{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000263#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000264 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 return( ioctlsocket( fd, FIONBIO, &n ) );
266#else
267 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
268#endif
269}
270
271int net_set_nonblock( int fd )
272{
Paul Bakkerff60ee62010-03-16 21:09:09 +0000273#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000274 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 return( ioctlsocket( fd, FIONBIO, &n ) );
276#else
277 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
278#endif
279}
280
281/*
282 * Portable usleep helper
283 */
284void net_usleep( unsigned long usec )
285{
286 struct timeval tv;
287 tv.tv_sec = 0;
288 tv.tv_usec = usec;
289 select( 0, NULL, NULL, NULL, &tv );
290}
291
292/*
293 * Read at most 'len' characters
294 */
Paul Bakker23986e52011-04-24 08:57:21 +0000295int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000296{
297 int ret = read( *((int *) ctx), buf, len );
298
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 if( ret < 0 )
300 {
301 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000302 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
Paul Bakkerff60ee62010-03-16 21:09:09 +0000304#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000306 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307#else
308 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000309 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000312 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313#endif
314
Paul Bakker40e46942009-01-03 21:51:57 +0000315 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 }
317
318 return( ret );
319}
320
321/*
322 * Write at most 'len' characters
323 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000324int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000325{
326 int ret = write( *((int *) ctx), buf, len );
327
328 if( ret < 0 )
329 {
330 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000331 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333#if defined(_WIN32) || defined(_WIN32_WCE)
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000335 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336#else
337 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000338 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000341 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342#endif
343
Paul Bakker40e46942009-01-03 21:51:57 +0000344 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 }
346
347 return( ret );
348}
349
350/*
351 * Gracefully close the connection
352 */
353void net_close( int fd )
354{
355 shutdown( fd, 2 );
356 close( fd );
357}
358
359#endif