blob: a37213446d553f386e4d685d974b7080e3452ff4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Manuel Pégourié-Gonnardf4acfe12014-09-17 10:56:54 +02002 * TCP/IP or UDP/IP networking functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
36 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010038#ifdef _WIN32_WINNT
39#undef _WIN32_WINNT
40#endif
41/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010042#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010043#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010044
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010045#include <winsock2.h>
46#include <windows.h>
47
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010048#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#if defined(_WIN32_WCE)
50#pragma comment( lib, "ws2.lib" )
51#else
52#pragma comment( lib, "ws2_32.lib" )
53#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010054#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkerf4f69682011-04-24 16:08:12 +000056#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
57#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000058#define close(fd) closesocket(fd)
59
60static int wsa_init_done = 0;
61
Paul Bakkerdb20c102014-06-17 14:34:44 +020062#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64#include <sys/types.h>
65#include <sys/socket.h>
66#include <netinet/in.h>
67#include <arpa/inet.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000069#include <sys/time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000071#include <unistd.h>
72#include <signal.h>
73#include <fcntl.h>
74#include <netdb.h>
75#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000076
Paul Bakkerdb20c102014-06-17 14:34:44 +020077#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Paul Bakker5121ce52009-01-03 21:22:43 +000079#include <stdlib.h>
80#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020081
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +010082#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
83 !defined(EFI32)
84#define snprintf _snprintf
85#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000088#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020089#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakkerfa6a6202013-10-28 18:48:30 +010091#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000092#include <basetsd.h>
93typedef UINT32 uint32_t;
94#else
95#include <inttypes.h>
96#endif
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000099#include "mbedtls/platform.h"
Rich Evansa18b11f2015-01-30 10:58:35 +0000100#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define mbedtls_snprintf snprintf
Rich Evansa18b11f2015-01-30 10:58:35 +0000102#endif
103
Paul Bakker5121ce52009-01-03 21:22:43 +0000104/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100105 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100107static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100109#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
110 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 WSADATA wsaData;
112
113 if( wsa_init_done == 0 )
114 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200115 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118 wsa_init_done = 1;
119 }
120#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100121#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 signal( SIGPIPE, SIG_IGN );
123#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100124#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100125 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100126}
127
128/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100129 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_net_connect( int *fd, const char *host, int port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100132{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100133 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100134 struct addrinfo hints, *addr_list, *cur;
135 char port_str[6];
136
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100137 if( ( ret = net_prepare() ) != 0 )
138 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100139
140 /* getaddrinfo expects port as a string */
141 memset( port_str, 0, sizeof( port_str ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100143
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100144 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100145 memset( &hints, 0, sizeof( hints ) );
146 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
148 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100149
150 if( getaddrinfo( host, port_str, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100152
153 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100155 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
156 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100157 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
158 cur->ai_protocol );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100159 if( *fd < 0 )
160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100162 continue;
163 }
164
165 if( connect( *fd, cur->ai_addr, cur->ai_addrlen ) == 0 )
166 {
167 ret = 0;
168 break;
169 }
170
171 close( *fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100173 }
174
175 freeaddrinfo( addr_list );
176
177 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178}
179
180/*
181 * Create a listening socket on bind_ip:port
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_net_bind( int *fd, const char *bind_ip, int port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100185 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100186 struct addrinfo hints, *addr_list, *cur;
187 char port_str[6];
188
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100189 if( ( ret = net_prepare() ) != 0 )
190 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100191
192 /* getaddrinfo expects port as a string */
193 memset( port_str, 0, sizeof( port_str ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_snprintf( port_str, sizeof( port_str ), "%d", port );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100195
196 /* Bind to IPv6 and/or IPv4, but only in TCP */
197 memset( &hints, 0, sizeof( hints ) );
198 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
200 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100201 if( bind_ip == NULL )
202 hints.ai_flags = AI_PASSIVE;
203
204 if( getaddrinfo( bind_ip, port_str, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100206
207 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100209 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
210 {
Paul Bakker00f5c522013-12-31 10:45:16 +0100211 *fd = (int) socket( cur->ai_family, cur->ai_socktype,
212 cur->ai_protocol );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100213 if( *fd < 0 )
214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100216 continue;
217 }
218
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100219 n = 1;
Paul Bakker874bd642014-04-17 12:43:05 +0200220 if( setsockopt( *fd, SOL_SOCKET, SO_REUSEADDR,
221 (const char *) &n, sizeof( n ) ) != 0 )
222 {
223 close( *fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200225 continue;
226 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100227
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100228 if( bind( *fd, cur->ai_addr, cur->ai_addrlen ) != 0 )
229 {
230 close( *fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100232 continue;
233 }
234
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100235 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( listen( *fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100239 {
240 close( *fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100242 continue;
243 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100244 }
245
246 /* I we ever get there, it's a success */
247 ret = 0;
248 break;
249 }
250
251 freeaddrinfo( addr_list );
252
253 return( ret );
254
Paul Bakker5121ce52009-01-03 21:22:43 +0000255}
256
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100257#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
258 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100259/*
260 * Check if the requested operation would be blocking on a non-blocking socket
261 * and thus 'failed' with a negative return value.
262 */
263static int net_would_block( int fd )
264{
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +0100265 ((void) fd);
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100267}
Paul Bakker5121ce52009-01-03 21:22:43 +0000268#else
Paul Bakker80025412014-01-23 20:59:49 +0100269/*
270 * Check if the requested operation would be blocking on a non-blocking socket
271 * and thus 'failed' with a negative return value.
272 *
273 * Note: on a blocking socket this function always returns 0!
274 */
275static int net_would_block( int fd )
276{
277 /*
278 * Never return 'WOULD BLOCK' on a non-blocking socket
279 */
280 if( ( fcntl( fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
281 return( 0 );
282
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 switch( errno )
284 {
285#if defined EAGAIN
286 case EAGAIN:
287#endif
288#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
289 case EWOULDBLOCK:
290#endif
291 return( 1 );
292 }
293 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200295#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297/*
298 * Accept a connection from a remote client
299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_net_accept( int bind_fd, int *client_fd, void *client_ip )
Paul Bakker5121ce52009-01-03 21:22:43 +0000301{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100302 int ret;
303 int type;
304
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100305 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
Paul Bakker394c56f2011-12-20 12:19:03 +0000307#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
308 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100310 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311#else
312 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100313 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314#endif
315
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100316 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard9325b262015-03-25 16:57:52 +0100317 if( getsockopt( bind_fd, SOL_SOCKET, SO_TYPE, (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100318 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100321 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100323 if( type == SOCK_STREAM )
324 {
325 /* TCP: actual accept() */
326 ret = *client_fd = (int) accept( bind_fd,
327 (struct sockaddr *) &client_addr, &n );
328 }
329 else
330 {
331 /* UDP: wait for a message, but keep it in the queue */
332 char buf[1] = { 0 };
333
Manuel Pégourié-Gonnardf3c500f2015-01-12 19:02:15 +0100334 ret = recvfrom( bind_fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100335 (struct sockaddr *) &client_addr, &n );
336 }
337
338 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 {
Manuel Pégourié-Gonnard9a6b4422014-07-21 13:42:54 +0200340 if( net_would_block( bind_fd ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100341 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 }
345
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100346 /* UDP: hijack the listening socket for communicating with the client */
347 if( type != SOCK_STREAM )
348 {
349 if( connect( bind_fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100351
352 *client_fd = bind_fd;
353 }
354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100356 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100357 if( client_addr.ss_family == AF_INET )
358 {
359 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
360 memcpy( client_ip, &addr4->sin_addr.s_addr,
361 sizeof( addr4->sin_addr.s_addr ) );
362 }
363 else
364 {
365 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
366 memcpy( client_ip, &addr6->sin6_addr.s6_addr,
367 sizeof( addr6->sin6_addr.s6_addr ) );
368 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100369 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371 return( 0 );
372}
373
374/*
375 * Set the socket blocking or non-blocking
376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_net_set_block( int fd )
Paul Bakker5121ce52009-01-03 21:22:43 +0000378{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100379#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
380 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000381 u_long n = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 return( ioctlsocket( fd, FIONBIO, &n ) );
383#else
384 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) & ~O_NONBLOCK ) );
385#endif
386}
387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388int mbedtls_net_set_nonblock( int fd )
Paul Bakker5121ce52009-01-03 21:22:43 +0000389{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100390#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
391 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000392 u_long n = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 return( ioctlsocket( fd, FIONBIO, &n ) );
394#else
395 return( fcntl( fd, F_SETFL, fcntl( fd, F_GETFL ) | O_NONBLOCK ) );
396#endif
397}
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000400/*
401 * Portable usleep helper
402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000404{
405 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100406 tv.tv_sec = usec / 1000000;
Paul Bakker82788fb2014-10-20 13:59:19 +0200407#if !defined(_WIN32) && ( defined(__unix__) || defined(__unix) || \
408 ( defined(__APPLE__) && defined(__MACH__) ) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100409 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200410#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100411 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200412#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 select( 0, NULL, NULL, NULL, &tv );
414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417/*
418 * Read at most 'len' characters
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100421{
Paul Bakker80025412014-01-23 20:59:49 +0100422 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200423 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 if( ret < 0 )
426 {
Paul Bakker80025412014-01-23 20:59:49 +0100427 if( net_would_block( fd ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100428 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100430#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
431 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434#else
435 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100439 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440#endif
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 }
444
445 return( ret );
446}
447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200449/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200450 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200453 uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200454{
455 int ret;
456 struct timeval tv;
457 fd_set read_fds;
458 int fd = *((int *) ctx);
459
460 FD_ZERO( &read_fds );
461 FD_SET( fd, &read_fds );
462
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200463 tv.tv_sec = timeout / 1000;
464 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200465
466 ret = select( fd + 1, &read_fds, NULL, NULL, &tv );
467
468 /* Zero fds ready means we timed out */
469 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100470 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200471
472 if( ret < 0 )
473 {
474#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
475 !defined(EFI32)
476 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100477 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200478#else
479 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100480 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200481#endif
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200484 }
485
486 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_HAVE_TIME */
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200490
Paul Bakker5121ce52009-01-03 21:22:43 +0000491/*
492 * Write at most 'len' characters
493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Paul Bakker80025412014-01-23 20:59:49 +0100496 int fd = *((int *) ctx);
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200497 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499 if( ret < 0 )
500 {
Paul Bakker80025412014-01-23 20:59:49 +0100501 if( net_would_block( fd ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100502 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100504#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
505 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508#else
509 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100513 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514#endif
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 }
518
519 return( ret );
520}
521
522/*
523 * Gracefully close the connection
524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525void mbedtls_net_close( int fd )
Paul Bakker5121ce52009-01-03 21:22:43 +0000526{
527 shutdown( fd, 2 );
528 close( fd );
529}
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#endif /* MBEDTLS_NET_C */