blob: 46902df551f359d54fa69638bc885e99b533e2b5 [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 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100183 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
184 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100185 if( *fd < 0 )
186 {
187 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
188 continue;
189 }
190
191 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
192 {
193 ret = 0;
194 break;
195 }
196
197 close( *fd );
198 ret = POLARSSL_ERR_NET_CONNECT_FAILED;
199 }
200
201 freeaddrinfo( addr_list );
202
203 return( ret );
204
205#else
206 /* Legacy IPv4-only version */
207
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100208 int ret;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100209 struct sockaddr_in server_addr;
210 struct hostent *server_host;
211
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100212 if( ( ret = net_prepare() ) != 0 )
213 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( ( server_host = gethostbyname( host ) ) == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +0000216 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakkerbbc10072013-10-14 16:33:24 +0200218 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000219 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 memcpy( (void *) &server_addr.sin_addr,
222 (void *) server_host->h_addr,
223 server_host->h_length );
224
225 server_addr.sin_family = AF_INET;
226 server_addr.sin_port = net_htons( port );
227
228 if( connect( *fd, (struct sockaddr *) &server_addr,
229 sizeof( server_addr ) ) < 0 )
230 {
231 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000232 return( POLARSSL_ERR_NET_CONNECT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 }
234
235 return( 0 );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100236#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000237}
238
239/*
240 * Create a listening socket on bind_ip:port
241 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000242int net_bind( int *fd, const char *bind_ip, int port )
Paul Bakker5121ce52009-01-03 21:22:43 +0000243{
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100244#if defined(POLARSSL_HAVE_IPV6)
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100245 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100246 struct addrinfo hints, *addr_list, *cur;
247 char port_str[6];
248
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100249 if( ( ret = net_prepare() ) != 0 )
250 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100251
252 /* getaddrinfo expects port as a string */
253 memset( port_str, 0, sizeof( port_str ) );
254 snprintf( port_str, sizeof( port_str ), "%d", port );
255
256 /* Bind to IPv6 and/or IPv4, but only in TCP */
257 memset( &hints, 0, sizeof( hints ) );
258 hints.ai_family = AF_UNSPEC;
259 hints.ai_socktype = SOCK_STREAM;
260 hints.ai_protocol = IPPROTO_TCP;
261 if( bind_ip == NULL )
262 hints.ai_flags = AI_PASSIVE;
263
264 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
265 return( POLARSSL_ERR_NET_UNKNOWN_HOST );
266
267 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100268 ret = POLARSSL_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
270 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100271 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
272 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100273 if( *fd < 0 )
274 {
275 ret = POLARSSL_ERR_NET_SOCKET_FAILED;
276 continue;
277 }
278
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100279 n = 1;
280 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
281 (const char *) &n, sizeof( n ) );
282
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100283 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
284 {
285 close( *fd );
286 ret = POLARSSL_ERR_NET_BIND_FAILED;
287 continue;
288 }
289
290 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
291 {
292 close( *fd );
293 ret = POLARSSL_ERR_NET_LISTEN_FAILED;
294 continue;
295 }
296
297 /* I we ever get there, it's a success */
298 ret = 0;
299 break;
300 }
301
302 freeaddrinfo( addr_list );
303
304 return( ret );
305
306#else
307 /* Legacy IPv4-only version */
308
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100309 int ret, n, c[4];
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 struct sockaddr_in server_addr;
311
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100312 if( ( ret = net_prepare() ) != 0 )
313 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Paul Bakkerbbc10072013-10-14 16:33:24 +0200315 if( ( *fd = (int) socket( AF_INET, SOCK_STREAM, IPPROTO_IP ) ) < 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000316 return( POLARSSL_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 n = 1;
319 setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
320 (const char *) &n, sizeof( n ) );
321
Paul Bakker37286a52013-03-06 16:55:11 +0100322 server_addr.sin_addr.s_addr = net_htonl( INADDR_ANY );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 server_addr.sin_family = AF_INET;
324 server_addr.sin_port = net_htons( port );
325
326 if( bind_ip != NULL )
327 {
328 memset( c, 0, sizeof( c ) );
329 sscanf( bind_ip, "%d.%d.%d.%d", &c[0], &c[1], &c[2], &c[3] );
330
331 for( n = 0; n < 4; n++ )
332 if( c[n] < 0 || c[n] > 255 )
333 break;
334
335 if( n == 4 )
Paul Bakker37286a52013-03-06 16:55:11 +0100336 server_addr.sin_addr.s_addr = net_htonl(
Paul Bakker5c2364c2012-10-01 14:41:15 +0000337 ( (uint32_t) c[0] << 24 ) |
338 ( (uint32_t) c[1] << 16 ) |
339 ( (uint32_t) c[2] << 8 ) |
Paul Bakker37286a52013-03-06 16:55:11 +0100340 ( (uint32_t) c[3] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 }
342
343 if( bind( *fd, (struct sockaddr *) &server_addr,
344 sizeof( server_addr ) ) < 0 )
345 {
346 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000347 return( POLARSSL_ERR_NET_BIND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 }
349
Paul Bakker192381a2011-05-20 12:31:31 +0000350 if( listen( *fd, POLARSSL_NET_LISTEN_BACKLOG ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 {
352 close( *fd );
Paul Bakker40e46942009-01-03 21:51:57 +0000353 return( POLARSSL_ERR_NET_LISTEN_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 }
355
356 return( 0 );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100357#endif /* POLARSSL_HAVE_IPV6 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000358}
359
360/*
361 * Check if the current operation is blocking
362 */
363static int net_is_blocking( void )
364{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100365#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
366 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 return( WSAGetLastError() == WSAEWOULDBLOCK );
368#else
369 switch( errno )
370 {
371#if defined EAGAIN
372 case EAGAIN:
373#endif
374#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
375 case EWOULDBLOCK:
376#endif
377 return( 1 );
378 }
379 return( 0 );
380#endif
381}
382
383/*
384 * Accept a connection from a remote client
385 */
386int net_accept( int bind_fd, int *client_fd, void *client_ip )
387{
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100388#if defined(POLARSSL_HAVE_IPV6)
389 struct sockaddr_storage client_addr;
390#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 struct sockaddr_in client_addr;
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100392#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
Paul Bakker394c56f2011-12-20 12:19:03 +0000394#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
395 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 socklen_t n = (socklen_t) sizeof( client_addr );
397#else
398 int n = (int) sizeof( client_addr );
399#endif
400
Paul Bakkerbbc10072013-10-14 16:33:24 +0200401 *client_fd = (int) accept( bind_fd, (struct sockaddr *)
402 &client_addr, &n );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
404 if( *client_fd < 0 )
405 {
406 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000407 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
Paul Bakker40e46942009-01-03 21:51:57 +0000409 return( POLARSSL_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 }
411
412 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100413 {
414#if defined(POLARSSL_HAVE_IPV6)
415 if( client_addr.ss_family == AF_INET )
416 {
417 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
418 memcpy( client_ip, &addr4->sin_addr.s_addr,
419 sizeof( addr4->sin_addr.s_addr ) );
420 }
421 else
422 {
423 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
424 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
425 sizeof( addr6->sin6_addr.s6_addr ) );
426 }
427#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 memcpy( client_ip, &client_addr.sin_addr.s_addr,
429 sizeof( client_addr.sin_addr.s_addr ) );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100430#endif /* POLARSSL_HAVE_IPV6 */
431 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 return( 0 );
434}
435
436/*
437 * Set the socket blocking or non-blocking
438 */
439int net_set_block( int fd )
440{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100441#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
442 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000443 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 return( ioctlsocket( fd, FIONBIO, &n ) );
445#else
446 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
447#endif
448}
449
450int net_set_nonblock( int fd )
451{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100452#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
453 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000454 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 return( ioctlsocket( fd, FIONBIO, &n ) );
456#else
457 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
458#endif
459}
460
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200461#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000462/*
463 * Portable usleep helper
464 */
465void net_usleep( unsigned long usec )
466{
467 struct timeval tv;
468 tv.tv_sec = 0;
469 tv.tv_usec = usec;
470 select( 0, NULL, NULL, NULL, &tv );
471}
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200472#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474/*
475 * Read at most 'len' characters
476 */
Paul Bakker23986e52011-04-24 08:57:21 +0000477int net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100478{
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 int ret = read( *((int *) ctx), buf, len );
480
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 if( ret < 0 )
482 {
483 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000484 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100486#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
487 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000489 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490#else
491 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000492 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000495 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496#endif
497
Paul Bakker40e46942009-01-03 21:51:57 +0000498 return( POLARSSL_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 }
500
501 return( ret );
502}
503
504/*
505 * Write at most 'len' characters
506 */
Paul Bakker39bb4182011-06-21 07:36:43 +0000507int net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508{
509 int ret = write( *((int *) ctx), buf, len );
510
511 if( ret < 0 )
512 {
513 if( net_is_blocking() != 0 )
Paul Bakker831a7552011-05-18 13:32:51 +0000514 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100516#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
517 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 if( WSAGetLastError() == WSAECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000519 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520#else
521 if( errno == EPIPE || errno == ECONNRESET )
Paul Bakker40e46942009-01-03 21:51:57 +0000522 return( POLARSSL_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 if( errno == EINTR )
Paul Bakker831a7552011-05-18 13:32:51 +0000525 return( POLARSSL_ERR_NET_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526#endif
527
Paul Bakker40e46942009-01-03 21:51:57 +0000528 return( POLARSSL_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 }
530
531 return( ret );
532}
533
534/*
535 * Gracefully close the connection
536 */
537void net_close( int fd )
538{
539 shutdown( fd, 2 );
540 close( fd );
541}
542
543#endif