blob: 67080a2b7663be81dd8530b2827011cb2476d3e8 [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 Bakkerf0fc2a22013-12-30 15:42:43 +010043#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000044#if defined(_WIN32_WCE)
45#pragma comment( lib, "ws2.lib" )
46#else
47#pragma comment( lib, "ws2_32.lib" )
48#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010049#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakkerf4f69682011-04-24 16:08:12 +000051#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
52#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000053#define close(fd) closesocket(fd)
54
55static int wsa_init_done = 0;
56
57#else
58
59#include <sys/types.h>
60#include <sys/socket.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020063#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000064#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000066#include <unistd.h>
67#include <signal.h>
68#include <fcntl.h>
69#include <netdb.h>
70#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000071
Paul Bakker6a2f8572012-08-23 07:45:37 +000072#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
73 defined(__DragonflyBSD__)
Paul Bakker854963c2009-07-19 20:50:11 +000074#include <sys/endian.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010075#elif defined(__APPLE__) || defined(HAVE_MACHINE_ENDIAN_H) || \
76 defined(EFIX64) || defined(EFI32)
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000077#include <machine/endian.h>
Paul Bakker61264812012-04-03 07:54:30 +000078#elif defined(sun)
79#include <sys/isa_defs.h>
Paul Bakker1e6a1752013-07-26 14:10:22 +020080#elif defined(_AIX) || defined(HAVE_ARPA_NAMESER_COMPAT_H)
81#include <arpa/nameser_compat.h>
Paul Bakker854963c2009-07-19 20:50:11 +000082#else
Paul Bakker1d4f30c2009-04-19 18:55:16 +000083#include <endian.h>
Paul Bakker854963c2009-07-19 20:50:11 +000084#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86#endif
87
Paul Bakker5121ce52009-01-03 21:22:43 +000088#include <stdlib.h>
89#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020090
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010091#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
92 !defined(EFI32)
93#define snprintf _snprintf
94#endif
95
Paul Bakkerfa9b1002013-07-03 15:31:03 +020096#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000097#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100100#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000101#include <basetsd.h>
102typedef UINT32 uint32_t;
103#else
104#include <inttypes.h>
105#endif
106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107/*
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000108 * htons() is not always available.
109 * By default go for LITTLE_ENDIAN variant. Otherwise hope for _BYTE_ORDER and __BIG_ENDIAN
Paul Bakker60b1d102013-10-29 10:02:51 +0100110 * to help determine endianness.
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000112#if defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000113#define POLARSSL_HTONS(n) (n)
Paul Bakker37286a52013-03-06 16:55:11 +0100114#define POLARSSL_HTONL(n) (n)
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000115#else
Paul Bakker37286a52013-03-06 16:55:11 +0100116#define POLARSSL_HTONS(n) ((((unsigned short)(n) & 0xFF ) << 8 ) | \
117 (((unsigned short)(n) & 0xFF00 ) >> 8 ))
118#define POLARSSL_HTONL(n) ((((unsigned long )(n) & 0xFF ) << 24) | \
119 (((unsigned long )(n) & 0xFF00 ) << 8 ) | \
120 (((unsigned long )(n) & 0xFF0000 ) >> 8 ) | \
121 (((unsigned long )(n) & 0xFF000000) >> 24))
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000122#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Paul Bakker1d4f30c2009-04-19 18:55:16 +0000124unsigned short net_htons(unsigned short n);
Paul Bakker37286a52013-03-06 16:55:11 +0100125unsigned long net_htonl(unsigned long n);
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000126#define net_htons(n) POLARSSL_HTONS(n)
Paul Bakker37286a52013-03-06 16:55:11 +0100127#define net_htonl(n) POLARSSL_HTONL(n)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100130 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100132static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100134#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
135 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 WSADATA wsaData;
137
138 if( wsa_init_done == 0 )
139 {
140 if( WSAStartup( MAKEWORD(2,0), &wsaData ) == SOCKET_ERROR )
Paul Bakker40e46942009-01-03 21:51:57 +0000141 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143 wsa_init_done = 1;
144 }
145#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100146#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 signal( SIGPIPE, SIG_IGN );
148#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100149#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100150 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100151}
152
153/*
154 * Initiate a TCP connection with host:port
155 */
156int net_connect( int *fd, const char *host, int port )
157{
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100158#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100159 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100160 struct addrinfo hints, *addr_list, *cur;
161 char port_str[6];
162
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100163 if( ( ret = net_prepare() ) != 0 )
164 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100165
166 /* getaddrinfo expects port as a string */
167 memset( port_str, 0, sizeof( port_str ) );
168 snprintf( port_str, sizeof( port_str ), "%d", port );
169
170 /* Do name resolution with both IPv6 and IPv4, but only TCP */
171 memset( &hints, 0, sizeof( hints ) );
172 hints.ai_family = AF_UNSPEC;
173 hints.ai_socktype = SOCK_STREAM;
174 hints.ai_protocol = IPPROTO_TCP;
175
176 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
177 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
178
179 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100180 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100181 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
182 {
183 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
184 if( *fd < 0 )
185 {
186 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
187 continue;
188 }
189
190 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
191 {
192 ret = 0;
193 break;
194 }
195
196 close( *fd );
197 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
198 }
199
200 freeaddrinfo( addr_list );
201
202 return( ret );
203
204#else
205 /* Legacy IPv4-only version */
206
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100207 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100208 struct sockaddr_in server_addr;
209 struct hostent *server_host;
210
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100211 if( ( ret = net_prepare() ) != 0 )
212 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000215 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
Paul Bakkerbbc10072013-10-14 16:33:24 +0200217 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000218 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 memcpy( (void *) &server_addr.sin_addr,
221 (void *) server_host->h_addr,
222 server_host->h_length );
223
224 server_addr.sin_family = AF_INET;
225 server_addr.sin_port = net_htons( port );
226
227 if( connect( *fd, (struct sockaddr *) &server_addr,
228 sizeof( server_addr ) ) < 0 )
229 {
230 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000231 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 }
233
234 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100235#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000236}
237
238/*
239 * Create a listening socket on bind_ip:port
240 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000241int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100243#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100244 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100245 struct addrinfo hints, *addr_list, *cur;
246 char port_str[6];
247
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100248 if( ( ret = net_prepare() ) != 0 )
249 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100250
251 /* getaddrinfo expects port as a string */
252 memset( port_str, 0, sizeof( port_str ) );
253 snprintf( port_str, sizeof( port_str ), "%d", port );
254
255 /* Bind to IPv6 and/or IPv4, but only in TCP */
256 memset( &hints, 0, sizeof( hints ) );
257 hints.ai_family = AF_UNSPEC;
258 hints.ai_socktype = SOCK_STREAM;
259 hints.ai_protocol = IPPROTO_TCP;
260 if( bind_ip == NULL )
261 hints.ai_flags = AI_PASSIVE;
262
263 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
264 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
265
266 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100267 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100268 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
269 {
270 *fd = socket( cur->ai_family, cur->ai_socktype, cur->ai_protocol );
271 if( *fd < 0 )
272 {
273 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
274 continue;
275 }
276
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100277 n = 1;
278 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
279 (const char *) &n, sizeof( n ) );
280
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100281 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
282 {
283 close( *fd );
284 ret = POLARSSL_ERR_NET_BIND_FAILED;
285 continue;
286 }
287
288 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
289 {
290 close( *fd );
291 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
292 continue;
293 }
294
295 /* I we ever get there, it's a success */
296 ret = 0;
297 break;
298 }
299
300 freeaddrinfo( addr_list );
301
302 return( ret );
303
304#else
305 /* Legacy IPv4-only version */
306
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100307 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 struct sockaddr_in server_addr;
309
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100310 if( ( ret = net_prepare() ) != 0 )
311 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Paul Bakkerbbc10072013-10-14 16:33:24 +0200313 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000314 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316 n = 1;
317 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
318 (const char *) &n, sizeof( n ) );
319
Paul Bakker37286a52013-03-06 16:55:11 +0100320 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 server_addr.sin_family = AF_INET;
322 server_addr.sin_port = net_htons( port );
323
324 if( bind_ip != NULL )
325 {
326 memset( c, 0, sizeof( c ) );
327 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
328
329 for( n = 0; n < 4; n++ )
330 if( c[n] < 0 || c[n] > 255 )
331 break;
332
333 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100334 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000335 ( (uint32_t) c[0] << 24 ) |
336 ( (uint32_t) c[1] << 16 ) |
337 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100338 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 }
340
341 if( bind( *fd, (struct sockaddr *) &server_addr,
342 sizeof( server_addr ) ) < 0 )
343 {
344 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000345 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 }
347
Paul Bakker192381a2011-05-20 12:31:31 +0000348 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 {
350 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000351 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 }
353
354 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100355#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000356}
357
358/*
359 * Check if the current operation is blocking
360 */
361static int net_is_blocking( void )
362{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100363#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
364 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 return( WSAGetLastError() == WSAEWOULDBLOCK );
366#else
367 switch( errno )
368 {
369#if defined EAGAIN
370 case EAGAIN:
371#endif
372#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
373 case EWOULDBLOCK:
374#endif
375 return( 1 );
376 }
377 return( 0 );
378#endif
379}
380
381/*
382 * Accept a connection from a remote client
383 */
384int net_accept( int bind_fd, int *client_fd, void *client_ip )
385{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100386#if defined(POLARSSL_HAVE_IPV6)
387 struct sockaddr_storage client_addr;
388#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100390#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Paul Bakker394c56f2011-12-20 12:19:03 +0000392#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
393 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 socklen_t n = (socklen_t) sizeof( client_addr );
395#else
396 int n = (int) sizeof( client_addr );
397#endif
398
Paul Bakkerbbc10072013-10-14 16:33:24 +0200399 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
400 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402 if( *client_fd < 0 )
403 {
404 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000405 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
Paul Bakker40e46942009-01-03 21:51:57 +0000407 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 }
409
410 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100411 {
412#if defined(POLARSSL_HAVE_IPV6)
413 if( client_addr.ss_family == AF_INET )
414 {
415 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
416 memcpy( client_ip, &addr4->sin_addr.s_addr,
417 sizeof( addr4->sin_addr.s_addr ) );
418 }
419 else
420 {
421 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
422 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
423 sizeof( addr6->sin6_addr.s6_addr ) );
424 }
425#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 memcpy( client_ip, &client_addr.sin_addr.s_addr,
427 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100428#endif /* POLARSSL_HAVE_IPV6 */
429 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431 return( 0 );
432}
433
434/*
435 * Set the socket blocking or non-blocking
436 */
437int net_set_block( int fd )
438{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100439#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
440 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000441 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 return( ioctlsocket( fd, FIONBIO, &n ) );
443#else
444 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
445#endif
446}
447
448int net_set_nonblock( int fd )
449{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100450#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
451 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000452 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 return( ioctlsocket( fd, FIONBIO, &n ) );
454#else
455 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
456#endif
457}
458
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200459#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000460/*
461 * Portable usleep helper
462 */
463void net_usleep( unsigned long usec )
464{
465 struct timeval tv;
466 tv.tv_sec = 0;
467 tv.tv_usec = usec;
468 select( 0, NULL, NULL, NULL, &tv );
469}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200470#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472/*
473 * Read at most 'len' characters
474 */
Paul Bakker23986e52011-04-24 08:57:21 +0000475int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100476{
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 int ret = read( *((int *) ctx), buf, len );
478
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 if( ret < 0 )
480 {
481 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000482 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100484#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
485 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000487 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488#else
489 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000490 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000493 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494#endif
495
Paul Bakker40e46942009-01-03 21:51:57 +0000496 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 }
498
499 return( ret );
500}
501
502/*
503 * Write at most 'len' characters
504 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000505int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506{
507 int ret = write( *((int *) ctx), buf, len );
508
509 if( ret < 0 )
510 {
511 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000512 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100514#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
515 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000517 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518#else
519 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000520 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000523 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524#endif
525
Paul Bakker40e46942009-01-03 21:51:57 +0000526 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 }
528
529 return( ret );
530}
531
532/*
533 * Gracefully close the connection
534 */
535void net_close( int fd )
536{
537 shutdown( fd, 2 );
538 close( fd );
539}
540
541#endif