blob: d02e3f269c96c9e25d3c56651566f420c33c2cc6 [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)
232 int ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
233 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
263 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
264 {
265 close( *fd );
266 ret = POLARSSL_ERR_NET_BIND_FAILED;
267 continue;
268 }
269
270 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
271 {
272 close( *fd );
273 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
274 continue;
275 }
276
277 /* I we ever get there, it's a success */
278 ret = 0;
279 break;
280 }
281
282 freeaddrinfo( addr_list );
283
284 return( ret );
285
286#else
287 /* Legacy IPv4-only version */
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 int n, c[4];
290 struct sockaddr_in server_addr;
291
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100292 net_prepare();
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Paul Bakkerbbc10072013-10-14 16:33:24 +0200294 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000295 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297 n = 1;
298 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
299 (const char *) &n, sizeof( n ) );
300
Paul Bakker37286a52013-03-06 16:55:11 +0100301 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 server_addr.sin_family = AF_INET;
303 server_addr.sin_port = net_htons( port );
304
305 if( bind_ip != NULL )
306 {
307 memset( c, 0, sizeof( c ) );
308 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
309
310 for( n = 0; n < 4; n++ )
311 if( c[n] < 0 || c[n] > 255 )
312 break;
313
314 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100315 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000316 ( (uint32_t) c[0] << 24 ) |
317 ( (uint32_t) c[1] << 16 ) |
318 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100319 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 }
321
322 if( bind( *fd, (struct sockaddr *) &server_addr,
323 sizeof( server_addr ) ) < 0 )
324 {
325 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000326 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 }
328
Paul Bakker192381a2011-05-20 12:31:31 +0000329 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 {
331 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000332 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 }
334
335 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100336#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000337}
338
339/*
340 * Check if the current operation is blocking
341 */
342static int net_is_blocking( void )
343{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100344#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
345 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 return( WSAGetLastError() == WSAEWOULDBLOCK );
347#else
348 switch( errno )
349 {
350#if defined EAGAIN
351 case EAGAIN:
352#endif
353#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
354 case EWOULDBLOCK:
355#endif
356 return( 1 );
357 }
358 return( 0 );
359#endif
360}
361
362/*
363 * Accept a connection from a remote client
364 */
365int net_accept( int bind_fd, int *client_fd, void *client_ip )
366{
367 struct sockaddr_in client_addr;
368
Paul Bakker394c56f2011-12-20 12:19:03 +0000369#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
370 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 socklen_t n = (socklen_t) sizeof( client_addr );
372#else
373 int n = (int) sizeof( client_addr );
374#endif
375
Paul Bakkerbbc10072013-10-14 16:33:24 +0200376 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
377 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379 if( *client_fd < 0 )
380 {
381 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000382 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Paul Bakker40e46942009-01-03 21:51:57 +0000384 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 }
386
387 if( client_ip != NULL )
388 memcpy( client_ip, &client_addr.sin_addr.s_addr,
389 sizeof( client_addr.sin_addr.s_addr ) );
390
391 return( 0 );
392}
393
394/*
395 * Set the socket blocking or non-blocking
396 */
397int net_set_block( int fd )
398{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100399#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
400 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000401 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 return( ioctlsocket( fd, FIONBIO, &n ) );
403#else
404 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
405#endif
406}
407
408int net_set_nonblock( int fd )
409{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100410#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
411 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000412 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 return( ioctlsocket( fd, FIONBIO, &n ) );
414#else
415 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
416#endif
417}
418
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200419#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000420/*
421 * Portable usleep helper
422 */
423void net_usleep( unsigned long usec )
424{
425 struct timeval tv;
426 tv.tv_sec = 0;
427 tv.tv_usec = usec;
428 select( 0, NULL, NULL, NULL, &tv );
429}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200430#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
432/*
433 * Read at most 'len' characters
434 */
Paul Bakker23986e52011-04-24 08:57:21 +0000435int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100436{
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 int ret = read( *((int *) ctx), buf, len );
438
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 if( ret < 0 )
440 {
441 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000442 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100444#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
445 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000447 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448#else
449 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000450 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000453 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454#endif
455
Paul Bakker40e46942009-01-03 21:51:57 +0000456 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 }
458
459 return( ret );
460}
461
462/*
463 * Write at most 'len' characters
464 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000465int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000466{
467 int ret = write( *((int *) ctx), buf, len );
468
469 if( ret < 0 )
470 {
471 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000472 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100474#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
475 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000477 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478#else
479 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000480 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000483 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484#endif
485
Paul Bakker40e46942009-01-03 21:51:57 +0000486 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 }
488
489 return( ret );
490}
491
492/*
493 * Gracefully close the connection
494 */
495void net_close( int fd )
496{
497 shutdown( fd, 2 );
498 close( fd );
499}
500
501#endif