blob: 19f699d506a02064790b0b4544d5d70bc13d6b9f [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
38#if defined(_WIN32_WCE)
39#pragma comment( lib, "ws2.lib" )
40#else
41#pragma comment( lib, "ws2_32.lib" )
42#endif
43
Paul Bakkerf4f69682011-04-24 16:08:12 +000044#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
45#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000046#define close(fd) closesocket(fd)
47
48static int wsa_init_done = 0;
49
50#else
51
52#include <sys/types.h>
53#include <sys/socket.h>
54#include <netinet/in.h>
55#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059#include <unistd.h>
60#include <signal.h>
61#include <fcntl.h>
62#include <netdb.h>
63#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000064
Paul Bakker6a2f8572012-08-23 07:45:37 +000065#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
66 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000067#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
69 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000070#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000071#elif defined(sun)
72#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020073#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
74#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000075#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000076#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79#endif
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081#include <stdlib.h>
82#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020083
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010084#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
85 !defined(EFI32)
86#define snprintf _snprintf
87#endif
88
Paul Bakkerfa9b1002013-07-03 15:31:03 +020089#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000090#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020091#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakkerfa6a6202013-10-28 18:48:30 +010093#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000094#include <basetsd.h>
95typedef UINT32 uint32_t;
96#else
97#include <inttypes.h>
98#endif
99
Paul Bakker5121ce52009-01-03 21:22:43 +0000100/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000101 * htons() is not always available.
102 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +0100103 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000105#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000106#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100107#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000108#else
Paul Bakker37286a52013-03-06 16:55:11 +0100109#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
110 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
111#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
112 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
113 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
114 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000115#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000117unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100118unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000119#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100120#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100123 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 */
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100125static void net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100127#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
128 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 WSADATA wsaData;
130
131 if( wsa_init_done == 0 )
132 {
133 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000134 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136 wsa_init_done = 1;
137 }
138#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100139#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 signal( SIGPIPE, SIG_IGN );
141#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100142#endif
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100143}
144
145/*
146 * Initiate a TCP connection with host:port
147 */
148int net_connect( int *fd, const char *host, int port )
149{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100150#if defined(POLARSSL_HAVE_IPV6)
151 int ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
152 struct addrinfo hints, *addr_list, *cur;
153 char port_str[6];
154
155 net_prepare();
156
157 /* getaddrinfo expects port as a string */
158 memset( port_str, 0, sizeof( port_str ) );
159 snprintf( port_str, sizeof( port_str ), "%d", port );
160
161 /* Do name resolution with both IPv6 and IPv4, but only TCP */
162 memset( &hints, 0, sizeof( hints ) );
163 hints.ai_family = AF_UNSPEC;
164 hints.ai_socktype = SOCK_STREAM;
165 hints.ai_protocol = IPPROTO_TCP;
166
167 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
168 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
169
170 /* Try the sockaddrs until a connection succeeds */
171 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
172 {
173 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
174 if( *fd < 0 )
175 {
176 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
177 continue;
178 }
179
180 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
181 {
182 ret = 0;
183 break;
184 }
185
186 close( *fd );
187 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
188 }
189
190 freeaddrinfo( addr_list );
191
192 return( ret );
193
194#else
195 /* Legacy IPv4-only version */
196
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100197 struct sockaddr_in server_addr;
198 struct hostent *server_host;
199
200 net_prepare();
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000203 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakkerbbc10072013-10-14 16:33:24 +0200205 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000206 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 memcpy( (void *) &server_addr.sin_addr,
209 (void *) server_host->h_addr,
210 server_host->h_length );
211
212 server_addr.sin_family = AF_INET;
213 server_addr.sin_port = net_htons( port );
214
215 if( connect( *fd, (struct sockaddr *) &server_addr,
216 sizeof( server_addr ) ) < 0 )
217 {
218 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000219 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 }
221
222 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100223#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000224}
225
226/*
227 * Create a listening socket on bind_ip:port
228 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100231#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100232 int n, ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100233 struct addrinfo hints, *addr_list, *cur;
234 char port_str[6];
235
236 net_prepare();
237
238 /* getaddrinfo expects port as a string */
239 memset( port_str, 0, sizeof( port_str ) );
240 snprintf( port_str, sizeof( port_str ), "%d", port );
241
242 /* Bind to IPv6 and/or IPv4, but only in TCP */
243 memset( &hints, 0, sizeof( hints ) );
244 hints.ai_family = AF_UNSPEC;
245 hints.ai_socktype = SOCK_STREAM;
246 hints.ai_protocol = IPPROTO_TCP;
247 if( bind_ip == NULL )
248 hints.ai_flags = AI_PASSIVE;
249
250 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
251 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
252
253 /* Try the sockaddrs until a binding succeeds */
254 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
255 {
256 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
257 if( *fd < 0 )
258 {
259 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
260 continue;
261 }
262
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100263 n = 1;
264 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
265 (const char *) &n, sizeof( n ) );
266
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100267 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
268 {
269 close( *fd );
270 ret = POLARSSL_ERR_NET_BIND_FAILED;
271 continue;
272 }
273
274 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
275 {
276 close( *fd );
277 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
278 continue;
279 }
280
281 /* I we ever get there, it's a success */
282 ret = 0;
283 break;
284 }
285
286 freeaddrinfo( addr_list );
287
288 return( ret );
289
290#else
291 /* Legacy IPv4-only version */
292
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 int n, c[4];
294 struct sockaddr_in server_addr;
295
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100296 net_prepare();
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Paul Bakkerbbc10072013-10-14 16:33:24 +0200298 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000299 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301 n = 1;
302 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
303 (const char *) &n, sizeof( n ) );
304
Paul Bakker37286a52013-03-06 16:55:11 +0100305 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 server_addr.sin_family = AF_INET;
307 server_addr.sin_port = net_htons( port );
308
309 if( bind_ip != NULL )
310 {
311 memset( c, 0, sizeof( c ) );
312 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
313
314 for( n = 0; n < 4; n++ )
315 if( c[n] < 0 || c[n] > 255 )
316 break;
317
318 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100319 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000320 ( (uint32_t) c[0] << 24 ) |
321 ( (uint32_t) c[1] << 16 ) |
322 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100323 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 }
325
326 if( bind( *fd, (struct sockaddr *) &server_addr,
327 sizeof( server_addr ) ) < 0 )
328 {
329 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000330 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 }
332
Paul Bakker192381a2011-05-20 12:31:31 +0000333 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 {
335 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000336 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 }
338
339 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100340#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000341}
342
343/*
344 * Check if the current operation is blocking
345 */
346static int net_is_blocking( void )
347{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100348#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
349 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 return( WSAGetLastError() == WSAEWOULDBLOCK );
351#else
352 switch( errno )
353 {
354#if defined EAGAIN
355 case EAGAIN:
356#endif
357#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
358 case EWOULDBLOCK:
359#endif
360 return( 1 );
361 }
362 return( 0 );
363#endif
364}
365
366/*
367 * Accept a connection from a remote client
368 */
369int net_accept( int bind_fd, int *client_fd, void *client_ip )
370{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100371#if defined(POLARSSL_HAVE_IPV6)
372 struct sockaddr_storage client_addr;
373#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Paul Bakker394c56f2011-12-20 12:19:03 +0000377#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
378 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 socklen_t n = (socklen_t) sizeof( client_addr );
380#else
381 int n = (int) sizeof( client_addr );
382#endif
383
Paul Bakkerbbc10072013-10-14 16:33:24 +0200384 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
385 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387 if( *client_fd < 0 )
388 {
389 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000390 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Paul Bakker40e46942009-01-03 21:51:57 +0000392 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 }
394
395 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100396 {
397#if defined(POLARSSL_HAVE_IPV6)
398 if( client_addr.ss_family == AF_INET )
399 {
400 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
401 memcpy( client_ip, &addr4->sin_addr.s_addr,
402 sizeof( addr4->sin_addr.s_addr ) );
403 }
404 else
405 {
406 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
407 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
408 sizeof( addr6->sin6_addr.s6_addr ) );
409 }
410#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 memcpy( client_ip, &client_addr.sin_addr.s_addr,
412 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100413#endif /* POLARSSL_HAVE_IPV6 */
414 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 return( 0 );
417}
418
419/*
420 * Set the socket blocking or non-blocking
421 */
422int net_set_block( int fd )
423{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100424#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
425 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000426 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 return( ioctlsocket( fd, FIONBIO, &n ) );
428#else
429 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
430#endif
431}
432
433int net_set_nonblock( int fd )
434{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100435#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
436 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000437 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 return( ioctlsocket( fd, FIONBIO, &n ) );
439#else
440 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
441#endif
442}
443
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200444#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000445/*
446 * Portable usleep helper
447 */
448void net_usleep( unsigned long usec )
449{
450 struct timeval tv;
451 tv.tv_sec = 0;
452 tv.tv_usec = usec;
453 select( 0, NULL, NULL, NULL, &tv );
454}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200455#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000456
457/*
458 * Read at most 'len' characters
459 */
Paul Bakker23986e52011-04-24 08:57:21 +0000460int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100461{
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 int ret = read( *((int *) ctx), buf, len );
463
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 if( ret < 0 )
465 {
466 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000467 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100469#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
470 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000472 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473#else
474 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000475 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000478 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479#endif
480
Paul Bakker40e46942009-01-03 21:51:57 +0000481 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 }
483
484 return( ret );
485}
486
487/*
488 * Write at most 'len' characters
489 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000490int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
492 int ret = write( *((int *) ctx), buf, len );
493
494 if( ret < 0 )
495 {
496 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000497 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100499#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
500 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000502 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503#else
504 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000505 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000508 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509#endif
510
Paul Bakker40e46942009-01-03 21:51:57 +0000511 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 }
513
514 return( ret );
515}
516
517/*
518 * Gracefully close the connection
519 */
520void net_close( int fd )
521{
522 shutdown( fd, 2 );
523 close( fd );
524}
525
526#endif