blob: 05280002c8acf6331b034e3a3c4276a1b1d81499 [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é-Gonnard173402b2013-12-17 15:57:05 +0100125static int 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
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100142 return( 0 );
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100143#endif
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100144}
145
146/*
147 * Initiate a TCP connection with host:port
148 */
149int net_connect( int *fd, const char *host, int port )
150{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100151#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100152 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100153 struct addrinfo hints, *addr_list, *cur;
154 char port_str[6];
155
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100156 if( ( ret = net_prepare() ) != 0 )
157 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100158
159 /* getaddrinfo expects port as a string */
160 memset( port_str, 0, sizeof( port_str ) );
161 snprintf( port_str, sizeof( port_str ), "%d", port );
162
163 /* Do name resolution with both IPv6 and IPv4, but only TCP */
164 memset( &hints, 0, sizeof( hints ) );
165 hints.ai_family = AF_UNSPEC;
166 hints.ai_socktype = SOCK_STREAM;
167 hints.ai_protocol = IPPROTO_TCP;
168
169 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
170 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
171
172 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100173 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100174 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
175 {
176 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
177 if( *fd < 0 )
178 {
179 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
180 continue;
181 }
182
183 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
184 {
185 ret = 0;
186 break;
187 }
188
189 close( *fd );
190 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
191 }
192
193 freeaddrinfo( addr_list );
194
195 return( ret );
196
197#else
198 /* Legacy IPv4-only version */
199
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100200 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100201 struct sockaddr_in server_addr;
202 struct hostent *server_host;
203
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100204 if( ( ret = net_prepare() ) != 0 )
205 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000208 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Paul Bakkerbbc10072013-10-14 16:33:24 +0200210 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000211 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 memcpy( (void *) &server_addr.sin_addr,
214 (void *) server_host->h_addr,
215 server_host->h_length );
216
217 server_addr.sin_family = AF_INET;
218 server_addr.sin_port = net_htons( port );
219
220 if( connect( *fd, (struct sockaddr *) &server_addr,
221 sizeof( server_addr ) ) < 0 )
222 {
223 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000224 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 }
226
227 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100228#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000229}
230
231/*
232 * Create a listening socket on bind_ip:port
233 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000234int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000235{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100236#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100237 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100238 struct addrinfo hints, *addr_list, *cur;
239 char port_str[6];
240
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100241 if( ( ret = net_prepare() ) != 0 )
242 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100243
244 /* getaddrinfo expects port as a string */
245 memset( port_str, 0, sizeof( port_str ) );
246 snprintf( port_str, sizeof( port_str ), "%d", port );
247
248 /* Bind to IPv6 and/or IPv4, but only in TCP */
249 memset( &hints, 0, sizeof( hints ) );
250 hints.ai_family = AF_UNSPEC;
251 hints.ai_socktype = SOCK_STREAM;
252 hints.ai_protocol = IPPROTO_TCP;
253 if( bind_ip == NULL )
254 hints.ai_flags = AI_PASSIVE;
255
256 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
257 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
258
259 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100260 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100261 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
262 {
263 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
264 if( *fd < 0 )
265 {
266 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
267 continue;
268 }
269
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100270 n = 1;
271 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
272 (const char *) &n, sizeof( n ) );
273
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100274 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
275 {
276 close( *fd );
277 ret = POLARSSL_ERR_NET_BIND_FAILED;
278 continue;
279 }
280
281 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
282 {
283 close( *fd );
284 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
285 continue;
286 }
287
288 /* I we ever get there, it's a success */
289 ret = 0;
290 break;
291 }
292
293 freeaddrinfo( addr_list );
294
295 return( ret );
296
297#else
298 /* Legacy IPv4-only version */
299
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100300 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 struct sockaddr_in server_addr;
302
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100303 if( ( ret = net_prepare() ) != 0 )
304 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
Paul Bakkerbbc10072013-10-14 16:33:24 +0200306 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000307 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309 n = 1;
310 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
311 (const char *) &n, sizeof( n ) );
312
Paul Bakker37286a52013-03-06 16:55:11 +0100313 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 server_addr.sin_family = AF_INET;
315 server_addr.sin_port = net_htons( port );
316
317 if( bind_ip != NULL )
318 {
319 memset( c, 0, sizeof( c ) );
320 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
321
322 for( n = 0; n < 4; n++ )
323 if( c[n] < 0 || c[n] > 255 )
324 break;
325
326 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100327 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000328 ( (uint32_t) c[0] << 24 ) |
329 ( (uint32_t) c[1] << 16 ) |
330 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100331 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 }
333
334 if( bind( *fd, (struct sockaddr *) &server_addr,
335 sizeof( server_addr ) ) < 0 )
336 {
337 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000338 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 }
340
Paul Bakker192381a2011-05-20 12:31:31 +0000341 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 {
343 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000344 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 }
346
347 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100348#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000349}
350
351/*
352 * Check if the current operation is blocking
353 */
354static int net_is_blocking( void )
355{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100356#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
357 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 return( WSAGetLastError() == WSAEWOULDBLOCK );
359#else
360 switch( errno )
361 {
362#if defined EAGAIN
363 case EAGAIN:
364#endif
365#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
366 case EWOULDBLOCK:
367#endif
368 return( 1 );
369 }
370 return( 0 );
371#endif
372}
373
374/*
375 * Accept a connection from a remote client
376 */
377int net_accept( int bind_fd, int *client_fd, void *client_ip )
378{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100379#if defined(POLARSSL_HAVE_IPV6)
380 struct sockaddr_storage client_addr;
381#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100383#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Paul Bakker394c56f2011-12-20 12:19:03 +0000385#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
386 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 socklen_t n = (socklen_t) sizeof( client_addr );
388#else
389 int n = (int) sizeof( client_addr );
390#endif
391
Paul Bakkerbbc10072013-10-14 16:33:24 +0200392 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
393 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395 if( *client_fd < 0 )
396 {
397 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000398 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Paul Bakker40e46942009-01-03 21:51:57 +0000400 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 }
402
403 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100404 {
405#if defined(POLARSSL_HAVE_IPV6)
406 if( client_addr.ss_family == AF_INET )
407 {
408 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
409 memcpy( client_ip, &addr4->sin_addr.s_addr,
410 sizeof( addr4->sin_addr.s_addr ) );
411 }
412 else
413 {
414 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
415 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
416 sizeof( addr6->sin6_addr.s6_addr ) );
417 }
418#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 memcpy( client_ip, &client_addr.sin_addr.s_addr,
420 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100421#endif /* POLARSSL_HAVE_IPV6 */
422 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 return( 0 );
425}
426
427/*
428 * Set the socket blocking or non-blocking
429 */
430int net_set_block( int fd )
431{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100432#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
433 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000434 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 return( ioctlsocket( fd, FIONBIO, &n ) );
436#else
437 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
438#endif
439}
440
441int net_set_nonblock( int fd )
442{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100443#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
444 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000445 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 return( ioctlsocket( fd, FIONBIO, &n ) );
447#else
448 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
449#endif
450}
451
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200452#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000453/*
454 * Portable usleep helper
455 */
456void net_usleep( unsigned long usec )
457{
458 struct timeval tv;
459 tv.tv_sec = 0;
460 tv.tv_usec = usec;
461 select( 0, NULL, NULL, NULL, &tv );
462}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200463#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465/*
466 * Read at most 'len' characters
467 */
Paul Bakker23986e52011-04-24 08:57:21 +0000468int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100469{
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 int ret = read( *((int *) ctx), buf, len );
471
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 if( ret < 0 )
473 {
474 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000475 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100477#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
478 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000480 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481#else
482 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000483 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000486 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487#endif
488
Paul Bakker40e46942009-01-03 21:51:57 +0000489 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 }
491
492 return( ret );
493}
494
495/*
496 * Write at most 'len' characters
497 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000498int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000499{
500 int ret = write( *((int *) ctx), buf, len );
501
502 if( ret < 0 )
503 {
504 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000505 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100507#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
508 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000510 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511#else
512 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000513 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000516 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517#endif
518
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 }
521
522 return( ret );
523}
524
525/*
526 * Gracefully close the connection
527 */
528void net_close( int fd )
529{
530 shutdown( fd, 2 );
531 close( fd );
532}
533
534#endif