blob: fd5fd127a072482be09e58ff7f247943f8441758 [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
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010035#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010036#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010037#include <ws2tcpip.h>
38#endif
39
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010040#include <winsock2.h>
41#include <windows.h>
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043#if defined(_WIN32_WCE)
44#pragma comment( lib, "ws2.lib" )
45#else
46#pragma comment( lib, "ws2_32.lib" )
47#endif
48
Paul Bakkerf4f69682011-04-24 16:08:12 +000049#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
50#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#define close(fd) closesocket(fd)
52
53static int wsa_init_done = 0;
54
55#else
56
57#include <sys/types.h>
58#include <sys/socket.h>
59#include <netinet/in.h>
60#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020061#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000062#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020063#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000064#include <unistd.h>
65#include <signal.h>
66#include <fcntl.h>
67#include <netdb.h>
68#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000069
Paul Bakker6a2f8572012-08-23 07:45:37 +000070#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
71 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000072#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010073#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
74 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000075#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000076#elif defined(sun)
77#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020078#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
79#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000080#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000081#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84#endif
85
Paul Bakker5121ce52009-01-03 21:22:43 +000086#include <stdlib.h>
87#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020088
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010089#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
90 !defined(EFI32)
91#define snprintf _snprintf
92#endif
93
Paul Bakkerfa9b1002013-07-03 15:31:03 +020094#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000095#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Paul Bakkerfa6a6202013-10-28 18:48:30 +010098#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000099#include <basetsd.h>
100typedef UINT32 uint32_t;
101#else
102#include <inttypes.h>
103#endif
104
Paul Bakker5121ce52009-01-03 21:22:43 +0000105/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000106 * htons() is not always available.
107 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +0100108 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000110#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000111#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100112#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000113#else
Paul Bakker37286a52013-03-06 16:55:11 +0100114#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
115 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
116#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
117 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
118 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
119 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000122unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100123unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000124#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100125#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100128 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100130static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100132#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
133 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 WSADATA wsaData;
135
136 if( wsa_init_done == 0 )
137 {
138 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000139 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 wsa_init_done = 1;
142 }
143#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100144#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 signal( SIGPIPE, SIG_IGN );
146#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100147#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100148 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100149}
150
151/*
152 * Initiate a TCP connection with host:port
153 */
154int net_connect( int *fd, const char *host, int port )
155{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100156#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100157 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100158 struct addrinfo hints, *addr_list, *cur;
159 char port_str[6];
160
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100161 if( ( ret = net_prepare() ) != 0 )
162 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100163
164 /* getaddrinfo expects port as a string */
165 memset( port_str, 0, sizeof( port_str ) );
166 snprintf( port_str, sizeof( port_str ), "%d", port );
167
168 /* Do name resolution with both IPv6 and IPv4, but only TCP */
169 memset( &hints, 0, sizeof( hints ) );
170 hints.ai_family = AF_UNSPEC;
171 hints.ai_socktype = SOCK_STREAM;
172 hints.ai_protocol = IPPROTO_TCP;
173
174 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
175 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
176
177 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100178 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100179 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
180 {
181 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
182 if( *fd < 0 )
183 {
184 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
185 continue;
186 }
187
188 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
189 {
190 ret = 0;
191 break;
192 }
193
194 close( *fd );
195 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
196 }
197
198 freeaddrinfo( addr_list );
199
200 return( ret );
201
202#else
203 /* Legacy IPv4-only version */
204
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100205 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100206 struct sockaddr_in server_addr;
207 struct hostent *server_host;
208
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100209 if( ( ret = net_prepare() ) != 0 )
210 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000213 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Paul Bakkerbbc10072013-10-14 16:33:24 +0200215 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000216 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218 memcpy( (void *) &server_addr.sin_addr,
219 (void *) server_host->h_addr,
220 server_host->h_length );
221
222 server_addr.sin_family = AF_INET;
223 server_addr.sin_port = net_htons( port );
224
225 if( connect( *fd, (struct sockaddr *) &server_addr,
226 sizeof( server_addr ) ) < 0 )
227 {
228 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000229 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 }
231
232 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100233#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000234}
235
236/*
237 * Create a listening socket on bind_ip:port
238 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000239int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100241#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100242 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100243 struct addrinfo hints, *addr_list, *cur;
244 char port_str[6];
245
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100246 if( ( ret = net_prepare() ) != 0 )
247 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100248
249 /* getaddrinfo expects port as a string */
250 memset( port_str, 0, sizeof( port_str ) );
251 snprintf( port_str, sizeof( port_str ), "%d", port );
252
253 /* Bind to IPv6 and/or IPv4, but only in TCP */
254 memset( &hints, 0, sizeof( hints ) );
255 hints.ai_family = AF_UNSPEC;
256 hints.ai_socktype = SOCK_STREAM;
257 hints.ai_protocol = IPPROTO_TCP;
258 if( bind_ip == NULL )
259 hints.ai_flags = AI_PASSIVE;
260
261 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
262 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
263
264 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100265 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100266 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
267 {
268 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
269 if( *fd < 0 )
270 {
271 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
272 continue;
273 }
274
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100275 n = 1;
276 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
277 (const char *) &n, sizeof( n ) );
278
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100279 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
280 {
281 close( *fd );
282 ret = POLARSSL_ERR_NET_BIND_FAILED;
283 continue;
284 }
285
286 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
287 {
288 close( *fd );
289 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
290 continue;
291 }
292
293 /* I we ever get there, it's a success */
294 ret = 0;
295 break;
296 }
297
298 freeaddrinfo( addr_list );
299
300 return( ret );
301
302#else
303 /* Legacy IPv4-only version */
304
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100305 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 struct sockaddr_in server_addr;
307
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100308 if( ( ret = net_prepare() ) != 0 )
309 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
Paul Bakkerbbc10072013-10-14 16:33:24 +0200311 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000312 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314 n = 1;
315 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
316 (const char *) &n, sizeof( n ) );
317
Paul Bakker37286a52013-03-06 16:55:11 +0100318 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 server_addr.sin_family = AF_INET;
320 server_addr.sin_port = net_htons( port );
321
322 if( bind_ip != NULL )
323 {
324 memset( c, 0, sizeof( c ) );
325 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
326
327 for( n = 0; n < 4; n++ )
328 if( c[n] < 0 || c[n] > 255 )
329 break;
330
331 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100332 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000333 ( (uint32_t) c[0] << 24 ) |
334 ( (uint32_t) c[1] << 16 ) |
335 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100336 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 }
338
339 if( bind( *fd, (struct sockaddr *) &server_addr,
340 sizeof( server_addr ) ) < 0 )
341 {
342 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 }
345
Paul Bakker192381a2011-05-20 12:31:31 +0000346 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 {
348 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000349 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 }
351
352 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100353#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000354}
355
356/*
357 * Check if the current operation is blocking
358 */
359static int net_is_blocking( void )
360{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100361#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
362 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 return( WSAGetLastError() == WSAEWOULDBLOCK );
364#else
365 switch( errno )
366 {
367#if defined EAGAIN
368 case EAGAIN:
369#endif
370#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
371 case EWOULDBLOCK:
372#endif
373 return( 1 );
374 }
375 return( 0 );
376#endif
377}
378
379/*
380 * Accept a connection from a remote client
381 */
382int net_accept( int bind_fd, int *client_fd, void *client_ip )
383{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100384#if defined(POLARSSL_HAVE_IPV6)
385 struct sockaddr_storage client_addr;
386#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100388#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
Paul Bakker394c56f2011-12-20 12:19:03 +0000390#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
391 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 socklen_t n = (socklen_t) sizeof( client_addr );
393#else
394 int n = (int) sizeof( client_addr );
395#endif
396
Paul Bakkerbbc10072013-10-14 16:33:24 +0200397 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
398 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400 if( *client_fd < 0 )
401 {
402 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000403 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
Paul Bakker40e46942009-01-03 21:51:57 +0000405 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 }
407
408 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100409 {
410#if defined(POLARSSL_HAVE_IPV6)
411 if( client_addr.ss_family == AF_INET )
412 {
413 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
414 memcpy( client_ip, &addr4->sin_addr.s_addr,
415 sizeof( addr4->sin_addr.s_addr ) );
416 }
417 else
418 {
419 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
420 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
421 sizeof( addr6->sin6_addr.s6_addr ) );
422 }
423#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 memcpy( client_ip, &client_addr.sin_addr.s_addr,
425 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100426#endif /* POLARSSL_HAVE_IPV6 */
427 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429 return( 0 );
430}
431
432/*
433 * Set the socket blocking or non-blocking
434 */
435int net_set_block( int fd )
436{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100437#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
438 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000439 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 return( ioctlsocket( fd, FIONBIO, &n ) );
441#else
442 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
443#endif
444}
445
446int net_set_nonblock( int fd )
447{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100448#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
449 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000450 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 return( ioctlsocket( fd, FIONBIO, &n ) );
452#else
453 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
454#endif
455}
456
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200457#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000458/*
459 * Portable usleep helper
460 */
461void net_usleep( unsigned long usec )
462{
463 struct timeval tv;
464 tv.tv_sec = 0;
465 tv.tv_usec = usec;
466 select( 0, NULL, NULL, NULL, &tv );
467}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200468#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470/*
471 * Read at most 'len' characters
472 */
Paul Bakker23986e52011-04-24 08:57:21 +0000473int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100474{
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 int ret = read( *((int *) ctx), buf, len );
476
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 if( ret < 0 )
478 {
479 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000480 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100482#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
483 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000485 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486#else
487 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000488 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000491 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492#endif
493
Paul Bakker40e46942009-01-03 21:51:57 +0000494 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 }
496
497 return( ret );
498}
499
500/*
501 * Write at most 'len' characters
502 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000503int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000504{
505 int ret = write( *((int *) ctx), buf, len );
506
507 if( ret < 0 )
508 {
509 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000510 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100512#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
513 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000515 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516#else
517 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000518 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
520 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000521 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522#endif
523
Paul Bakker40e46942009-01-03 21:51:57 +0000524 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 }
526
527 return( ret );
528}
529
530/*
531 * Gracefully close the connection
532 */
533void net_close( int fd )
534{
535 shutdown( fd, 2 );
536 close( fd );
537}
538
539#endif