blob: e666fadf1193d192398bac001e248ec4560c601c [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 Bakkerfa6a6202013-10-28 18:48:30 +010032#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
33 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35#include <winsock2.h>
36#include <windows.h>
37
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010038#if defined(POLARSSL_HAVE_IPV6)
39#include <ws2tcpip.h>
40#endif
41
Paul Bakker5121ce52009-01-03 21:22:43 +000042#if defined(_WIN32_WCE)
43#pragma comment( lib, "ws2.lib" )
44#else
45#pragma comment( lib, "ws2_32.lib" )
46#endif
47
Paul Bakkerf4f69682011-04-24 16:08:12 +000048#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
49#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000050#define close(fd) closesocket(fd)
51
52static int wsa_init_done = 0;
53
54#else
55
56#include <sys/types.h>
57#include <sys/socket.h>
58#include <netinet/in.h>
59#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020060#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000061#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000063#include <unistd.h>
64#include <signal.h>
65#include <fcntl.h>
66#include <netdb.h>
67#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000068
Paul Bakker6a2f8572012-08-23 07:45:37 +000069#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
70 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000071#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010072#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
73 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000074#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000075#elif defined(sun)
76#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020077#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
78#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000079#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000080#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83#endif
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085#include <stdlib.h>
86#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020087
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010088#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
89 !defined(EFI32)
90#define snprintf _snprintf
91#endif
92
Paul Bakkerfa9b1002013-07-03 15:31:03 +020093#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000094#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020095#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakkerfa6a6202013-10-28 18:48:30 +010097#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000098#include <basetsd.h>
99typedef UINT32 uint32_t;
100#else
101#include <inttypes.h>
102#endif
103
Paul Bakker5121ce52009-01-03 21:22:43 +0000104/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000105 * htons() is not always available.
106 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +0100107 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000109#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000110#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100111#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000112#else
Paul Bakker37286a52013-03-06 16:55:11 +0100113#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
114 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
115#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
116 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
117 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
118 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000119#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000121unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100122unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000123#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100124#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100127 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100129static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100131#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
132 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 WSADATA wsaData;
134
135 if( wsa_init_done == 0 )
136 {
137 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000138 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140 wsa_init_done = 1;
141 }
142#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100143#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 signal( SIGPIPE, SIG_IGN );
145#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100146#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100147 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100148}
149
150/*
151 * Initiate a TCP connection with host:port
152 */
153int net_connect( int *fd, const char *host, int port )
154{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100155#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100156 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100157 struct addrinfo hints, *addr_list, *cur;
158 char port_str[6];
159
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100160 if( ( ret = net_prepare() ) != 0 )
161 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100162
163 /* getaddrinfo expects port as a string */
164 memset( port_str, 0, sizeof( port_str ) );
165 snprintf( port_str, sizeof( port_str ), "%d", port );
166
167 /* Do name resolution with both IPv6 and IPv4, but only TCP */
168 memset( &hints, 0, sizeof( hints ) );
169 hints.ai_family = AF_UNSPEC;
170 hints.ai_socktype = SOCK_STREAM;
171 hints.ai_protocol = IPPROTO_TCP;
172
173 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
174 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
175
176 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100177 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100178 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
179 {
180 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
181 if( *fd < 0 )
182 {
183 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
184 continue;
185 }
186
187 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
188 {
189 ret = 0;
190 break;
191 }
192
193 close( *fd );
194 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
195 }
196
197 freeaddrinfo( addr_list );
198
199 return( ret );
200
201#else
202 /* Legacy IPv4-only version */
203
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100204 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100205 struct sockaddr_in server_addr;
206 struct hostent *server_host;
207
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100208 if( ( ret = net_prepare() ) != 0 )
209 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000212 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Paul Bakkerbbc10072013-10-14 16:33:24 +0200214 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000215 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 memcpy( (void *) &server_addr.sin_addr,
218 (void *) server_host->h_addr,
219 server_host->h_length );
220
221 server_addr.sin_family = AF_INET;
222 server_addr.sin_port = net_htons( port );
223
224 if( connect( *fd, (struct sockaddr *) &server_addr,
225 sizeof( server_addr ) ) < 0 )
226 {
227 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000228 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 }
230
231 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100232#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000233}
234
235/*
236 * Create a listening socket on bind_ip:port
237 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000238int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000239{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100240#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100241 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100242 struct addrinfo hints, *addr_list, *cur;
243 char port_str[6];
244
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100245 if( ( ret = net_prepare() ) != 0 )
246 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100247
248 /* getaddrinfo expects port as a string */
249 memset( port_str, 0, sizeof( port_str ) );
250 snprintf( port_str, sizeof( port_str ), "%d", port );
251
252 /* Bind to IPv6 and/or IPv4, but only in TCP */
253 memset( &hints, 0, sizeof( hints ) );
254 hints.ai_family = AF_UNSPEC;
255 hints.ai_socktype = SOCK_STREAM;
256 hints.ai_protocol = IPPROTO_TCP;
257 if( bind_ip == NULL )
258 hints.ai_flags = AI_PASSIVE;
259
260 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
261 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
262
263 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100264 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100265 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
266 {
267 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
268 if( *fd < 0 )
269 {
270 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
271 continue;
272 }
273
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100274 n = 1;
275 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
276 (const char *) &n, sizeof( n ) );
277
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100278 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
279 {
280 close( *fd );
281 ret = POLARSSL_ERR_NET_BIND_FAILED;
282 continue;
283 }
284
285 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
286 {
287 close( *fd );
288 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
289 continue;
290 }
291
292 /* I we ever get there, it's a success */
293 ret = 0;
294 break;
295 }
296
297 freeaddrinfo( addr_list );
298
299 return( ret );
300
301#else
302 /* Legacy IPv4-only version */
303
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100304 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 struct sockaddr_in server_addr;
306
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100307 if( ( ret = net_prepare() ) != 0 )
308 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Paul Bakkerbbc10072013-10-14 16:33:24 +0200310 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000311 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 n = 1;
314 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
315 (const char *) &n, sizeof( n ) );
316
Paul Bakker37286a52013-03-06 16:55:11 +0100317 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 server_addr.sin_family = AF_INET;
319 server_addr.sin_port = net_htons( port );
320
321 if( bind_ip != NULL )
322 {
323 memset( c, 0, sizeof( c ) );
324 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
325
326 for( n = 0; n < 4; n++ )
327 if( c[n] < 0 || c[n] > 255 )
328 break;
329
330 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100331 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000332 ( (uint32_t) c[0] << 24 ) |
333 ( (uint32_t) c[1] << 16 ) |
334 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100335 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 }
337
338 if( bind( *fd, (struct sockaddr *) &server_addr,
339 sizeof( server_addr ) ) < 0 )
340 {
341 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000342 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 }
344
Paul Bakker192381a2011-05-20 12:31:31 +0000345 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 {
347 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000348 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 }
350
351 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100352#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000353}
354
355/*
356 * Check if the current operation is blocking
357 */
358static int net_is_blocking( void )
359{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100360#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
361 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 return( WSAGetLastError() == WSAEWOULDBLOCK );
363#else
364 switch( errno )
365 {
366#if defined EAGAIN
367 case EAGAIN:
368#endif
369#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
370 case EWOULDBLOCK:
371#endif
372 return( 1 );
373 }
374 return( 0 );
375#endif
376}
377
378/*
379 * Accept a connection from a remote client
380 */
381int net_accept( int bind_fd, int *client_fd, void *client_ip )
382{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100383#if defined(POLARSSL_HAVE_IPV6)
384 struct sockaddr_storage client_addr;
385#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100387#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Paul Bakker394c56f2011-12-20 12:19:03 +0000389#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
390 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 socklen_t n = (socklen_t) sizeof( client_addr );
392#else
393 int n = (int) sizeof( client_addr );
394#endif
395
Paul Bakkerbbc10072013-10-14 16:33:24 +0200396 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
397 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399 if( *client_fd < 0 )
400 {
401 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000402 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
Paul Bakker40e46942009-01-03 21:51:57 +0000404 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 }
406
407 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100408 {
409#if defined(POLARSSL_HAVE_IPV6)
410 if( client_addr.ss_family == AF_INET )
411 {
412 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
413 memcpy( client_ip, &addr4->sin_addr.s_addr,
414 sizeof( addr4->sin_addr.s_addr ) );
415 }
416 else
417 {
418 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
419 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
420 sizeof( addr6->sin6_addr.s6_addr ) );
421 }
422#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 memcpy( client_ip, &client_addr.sin_addr.s_addr,
424 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100425#endif /* POLARSSL_HAVE_IPV6 */
426 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428 return( 0 );
429}
430
431/*
432 * Set the socket blocking or non-blocking
433 */
434int net_set_block( int fd )
435{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100436#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
437 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000438 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 return( ioctlsocket( fd, FIONBIO, &n ) );
440#else
441 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
442#endif
443}
444
445int net_set_nonblock( int fd )
446{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100447#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
448 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000449 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 return( ioctlsocket( fd, FIONBIO, &n ) );
451#else
452 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
453#endif
454}
455
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200456#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000457/*
458 * Portable usleep helper
459 */
460void net_usleep( unsigned long usec )
461{
462 struct timeval tv;
463 tv.tv_sec = 0;
464 tv.tv_usec = usec;
465 select( 0, NULL, NULL, NULL, &tv );
466}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200467#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469/*
470 * Read at most 'len' characters
471 */
Paul Bakker23986e52011-04-24 08:57:21 +0000472int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100473{
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 int ret = read( *((int *) ctx), buf, len );
475
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 if( ret < 0 )
477 {
478 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000479 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100481#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
482 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000484 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485#else
486 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000487 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000490 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491#endif
492
Paul Bakker40e46942009-01-03 21:51:57 +0000493 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 }
495
496 return( ret );
497}
498
499/*
500 * Write at most 'len' characters
501 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000502int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000503{
504 int ret = write( *((int *) ctx), buf, len );
505
506 if( ret < 0 )
507 {
508 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000509 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100511#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
512 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000514 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515#else
516 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000517 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000520 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521#endif
522
Paul Bakker40e46942009-01-03 21:51:57 +0000523 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 }
525
526 return( ret );
527}
528
529/*
530 * Gracefully close the connection
531 */
532void net_close( int fd )
533{
534 shutdown( fd, 2 );
535 close( fd );
536}
537
538#endif