blob: e5efce0102b6361f637fd966d1c2acebae42af07 [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é-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010030#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040031 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
32 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010033#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
34#endif
35
SimonBd5800b72016-04-26 07:43:27 +010036#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010040#endif
41
Andres AG788aa4a2016-09-14 14:32:09 +010042#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <string.h>
45
Paul Bakkerfa6a6202013-10-28 18:48:30 +010046#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
47 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010049#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000050
Fabio Alessandrellidf608562018-04-03 19:40:11 +020051#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010052#undef _WIN32_WINNT
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010053/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010054#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020055#endif
56
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010057#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010058
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010059#include <winsock2.h>
60#include <windows.h>
61
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010062#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000063#if defined(_WIN32_WCE)
64#pragma comment( lib, "ws2.lib" )
65#else
66#pragma comment( lib, "ws2_32.lib" )
67#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010068#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010070#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
71#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000072#define close(fd) closesocket(fd)
73
74static int wsa_init_done = 0;
75
Paul Bakkerdb20c102014-06-17 14:34:44 +020076#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78#include <sys/types.h>
79#include <sys/socket.h>
80#include <netinet/in.h>
81#include <arpa/inet.h>
82#include <sys/time.h>
83#include <unistd.h>
84#include <signal.h>
85#include <fcntl.h>
86#include <netdb.h>
87#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000088
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010089#define IS_EINTR( ret ) ( ( ret ) == EINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000090
Paul Bakkerdb20c102014-06-17 14:34:44 +020091#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020093/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010094 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020095#if defined(_MSC_VER)
96#define MSVC_INT_CAST (int)
97#else
98#define MSVC_INT_CAST
99#endif
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200102
Paul Bakker5121ce52009-01-03 21:22:43 +0000103#include <time.h>
104
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200105#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000106
Paul Bakker5121ce52009-01-03 21:22:43 +0000107/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100108 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100110static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100112#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
113 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 WSADATA wsaData;
115
116 if( wsa_init_done == 0 )
117 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200118 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 wsa_init_done = 1;
122 }
123#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100124#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 signal( SIGPIPE, SIG_IGN );
126#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100127#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100128 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100129}
130
131/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200132 * Initialize a context
133 */
134void mbedtls_net_init( mbedtls_net_context *ctx )
135{
136 ctx->fd = -1;
137}
138
139/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100140 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100141 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000142int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
143 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100144{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100145 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100146 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100147
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100148 if( ( ret = net_prepare() ) != 0 )
149 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100150
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100151 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100152 memset( &hints, 0, sizeof( hints ) );
153 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
155 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100156
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200157 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100159
160 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100162 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
163 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200164 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100165 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200166 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100169 continue;
170 }
171
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200172 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100173 {
174 ret = 0;
175 break;
176 }
177
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200178 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100180 }
181
182 freeaddrinfo( addr_list );
183
184 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185}
186
187/*
188 * Create a listening socket on bind_ip:port
189 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200190int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100192 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100193 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100194
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100195 if( ( ret = net_prepare() ) != 0 )
196 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100197
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200198 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100199 memset( &hints, 0, sizeof( hints ) );
200 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
202 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100203 if( bind_ip == NULL )
204 hints.ai_flags = AI_PASSIVE;
205
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200206 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100208
209 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100211 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
212 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200213 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100214 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200215 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100218 continue;
219 }
220
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100221 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200222 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200223 (const char *) &n, sizeof( n ) ) != 0 )
224 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200225 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200227 continue;
228 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100229
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200230 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100231 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200232 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100234 continue;
235 }
236
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100237 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100239 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200240 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100241 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200242 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100244 continue;
245 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100246 }
247
Brian J Murray2adecba2016-11-06 04:45:15 -0800248 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100249 ret = 0;
250 break;
251 }
252
253 freeaddrinfo( addr_list );
254
255 return( ret );
256
Paul Bakker5121ce52009-01-03 21:22:43 +0000257}
258
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100259#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
260 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100261/*
262 * Check if the requested operation would be blocking on a non-blocking socket
263 * and thus 'failed' with a negative return value.
264 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200265static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100266{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200267 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100269}
Paul Bakker5121ce52009-01-03 21:22:43 +0000270#else
Paul Bakker80025412014-01-23 20:59:49 +0100271/*
272 * Check if the requested operation would be blocking on a non-blocking socket
273 * and thus 'failed' with a negative return value.
274 *
275 * Note: on a blocking socket this function always returns 0!
276 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200277static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100278{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100279 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100280
Paul Bakker80025412014-01-23 20:59:49 +0100281 /*
282 * Never return 'WOULD BLOCK' on a non-blocking socket
283 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200284 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100285 {
286 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100287 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100288 }
Paul Bakker80025412014-01-23 20:59:49 +0100289
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100290 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 {
292#if defined EAGAIN
293 case EAGAIN:
294#endif
295#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
296 case EWOULDBLOCK:
297#endif
298 return( 1 );
299 }
300 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200302#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304/*
305 * Accept a connection from a remote client
306 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200307int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
308 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200309 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000310{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100311 int ret;
312 int type;
313
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100314 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Paul Bakker394c56f2011-12-20 12:19:03 +0000316#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
Manuel Pégourié-Gonnard04317352015-10-05 12:16:06 +0100317 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100319 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320#else
321 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100322 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323#endif
324
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100325 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200326 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
327 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100328 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100331 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100333 if( type == SOCK_STREAM )
334 {
335 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200336 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000337 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100338 }
339 else
340 {
341 /* UDP: wait for a message, but keep it in the queue */
342 char buf[1] = { 0 };
343
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700344 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100345 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200346
347#if defined(_WIN32)
348 if( ret == SOCKET_ERROR &&
349 WSAGetLastError() == WSAEMSGSIZE )
350 {
351 /* We know buf is too small, thanks, just peeking here */
352 ret = 0;
353 }
354#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100355 }
356
357 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200359 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100360 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 }
364
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200365 /* UDP: hijack the listening socket to communicate with the client,
366 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100367 if( type != SOCK_STREAM )
368 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200369 struct sockaddr_storage local_addr;
370 int one = 1;
371
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200372 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100374
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200375 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200376 bind_ctx->fd = -1; /* In case we exit early */
377
378 n = sizeof( struct sockaddr_storage );
379 if( getsockname( client_ctx->fd,
380 (struct sockaddr *) &local_addr, &n ) != 0 ||
381 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
382 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
383 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
384 (const char *) &one, sizeof( one ) ) != 0 )
385 {
386 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
387 }
388
389 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
390 {
391 return( MBEDTLS_ERR_NET_BIND_FAILED );
392 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100393 }
394
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100396 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100397 if( client_addr.ss_family == AF_INET )
398 {
399 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200400 *ip_len = sizeof( addr4->sin_addr.s_addr );
401
402 if( buf_size < *ip_len )
403 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
404
405 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100406 }
407 else
408 {
409 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200410 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
411
412 if( buf_size < *ip_len )
413 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
414
415 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100416 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100417 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 return( 0 );
420}
421
422/*
423 * Set the socket blocking or non-blocking
424 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200425int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000426{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100427#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
428 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000429 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200430 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200432 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433#endif
434}
435
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200436int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000437{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100438#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
439 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000440 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200441 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200443 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444#endif
445}
446
447/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100448 * Check if data is available on the socket
449 */
450
451int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
452{
453 int ret;
454 struct timeval tv;
455
456 fd_set read_fds;
457 fd_set write_fds;
458
459 int fd = ctx->fd;
460
461 if( fd < 0 )
462 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
463
Gilles Peskineec4733b2018-04-05 14:55:47 +0200464#if defined(__has_feature)
465#if __has_feature(memory_sanitizer)
466 /* Ensure that memory sanitizers consider read_fds and write_fds as
467 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
468 * is implemented in assembly. */
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100469 memset( &read_fds, 0, sizeof( read_fds ) );
470 memset( &write_fds, 0, sizeof( write_fds ) );
Gilles Peskineec4733b2018-04-05 14:55:47 +0200471#endif
472#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100473
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100474 FD_ZERO( &read_fds );
475 if( rw & MBEDTLS_NET_POLL_READ )
476 {
477 rw &= ~MBEDTLS_NET_POLL_READ;
478 FD_SET( fd, &read_fds );
479 }
480
481 FD_ZERO( &write_fds );
482 if( rw & MBEDTLS_NET_POLL_WRITE )
483 {
484 rw &= ~MBEDTLS_NET_POLL_WRITE;
485 FD_SET( fd, &write_fds );
486 }
487
488 if( rw != 0 )
489 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
490
491 tv.tv_sec = timeout / 1000;
492 tv.tv_usec = ( timeout % 1000 ) * 1000;
493
Hanno Becker9ac64032018-03-15 12:19:31 +0000494 do
495 {
496 ret = select( fd + 1, &read_fds, &write_fds, NULL,
497 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000498 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100499 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100500
501 if( ret < 0 )
502 return( MBEDTLS_ERR_NET_POLL_FAILED );
503
504 ret = 0;
505 if( FD_ISSET( fd, &read_fds ) )
506 ret |= MBEDTLS_NET_POLL_READ;
507 if( FD_ISSET( fd, &write_fds ) )
508 ret |= MBEDTLS_NET_POLL_WRITE;
509
510 return( ret );
511}
512
513/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 * Portable usleep helper
515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000517{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200518#if defined(_WIN32)
519 Sleep( ( usec + 999 ) / 1000 );
520#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100522 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200523#if defined(__unix__) || defined(__unix) || \
524 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100525 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200526#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100527 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200528#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200530#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000531}
532
533/*
534 * Read at most 'len' characters
535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100537{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200538 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200539 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200540
541 if( fd < 0 )
542 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
543
544 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 if( ret < 0 )
547 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200548 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100549 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100551#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
552 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555#else
556 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100560 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561#endif
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 }
565
566 return( ret );
567}
568
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200569/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200570 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200571 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100572int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
573 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200574{
575 int ret;
576 struct timeval tv;
577 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200578 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200579
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200580 if( fd < 0 )
581 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
582
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200583 FD_ZERO( &read_fds );
584 FD_SET( fd, &read_fds );
585
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200586 tv.tv_sec = timeout / 1000;
587 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200588
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200589 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200590
591 /* Zero fds ready means we timed out */
592 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100593 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200594
595 if( ret < 0 )
596 {
597#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
598 !defined(EFI32)
599 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100600 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200601#else
602 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100603 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200604#endif
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200607 }
608
609 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200611}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200612
Paul Bakker5121ce52009-01-03 21:22:43 +0000613/*
614 * Write at most 'len' characters
615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000617{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200618 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200619 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200620
621 if( fd < 0 )
622 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
623
624 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 if( ret < 0 )
627 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200628 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100629 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100631#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
632 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635#else
636 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
639 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100640 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641#endif
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 }
645
646 return( ret );
647}
648
649/*
650 * Gracefully close the connection
651 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200652void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000653{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200654 if( ctx->fd == -1 )
655 return;
656
657 shutdown( ctx->fd, 2 );
658 close( ctx->fd );
659
660 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000661}
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_NET_C */