blob: b26e85818563f8b40207f64372f0f577dd86b374 [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
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
23 * be set before config.h, which pulls in glibc's features.h indirectly.
24 * Harmless on other platforms. */
25#define _POSIX_C_SOURCE 200112L
nia0b01fd92020-06-11 12:29:15 +010026#define _XOPEN_SOURCE 600 /* sockaddr_storage */
Nicholas Wilson2682edf2017-12-05 12:08:15 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010036#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040037 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000038 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010039#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
40#endif
41
SimonBd5800b72016-04-26 07:43:27 +010042#if defined(MBEDTLS_PLATFORM_C)
43#include "mbedtls/platform.h"
44#else
45#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010046#endif
47
Andres AG788aa4a2016-09-14 14:32:09 +010048#include "mbedtls/net_sockets.h"
Janos Follath73c616b2019-12-18 15:07:04 +000049#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Paul Bakkerfa6a6202013-10-28 18:48:30 +010053#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
54 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010056#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000057
opatomic2b2acf12020-04-24 10:00:36 -040058#if !defined(_WIN32_WINNT)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010059/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010060#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020061#endif
62
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010063#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010064
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010065#include <winsock2.h>
66#include <windows.h>
opatomic2b2acf12020-04-24 10:00:36 -040067#if (_WIN32_WINNT < 0x0501)
68#include <wspiapi.h>
69#endif
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010070
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010071#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000072#if defined(_WIN32_WCE)
73#pragma comment( lib, "ws2.lib" )
74#else
75#pragma comment( lib, "ws2_32.lib" )
76#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010077#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010079#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
80#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000081#define close(fd) closesocket(fd)
82
83static int wsa_init_done = 0;
84
Paul Bakkerdb20c102014-06-17 14:34:44 +020085#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87#include <sys/types.h>
88#include <sys/socket.h>
89#include <netinet/in.h>
90#include <arpa/inet.h>
91#include <sys/time.h>
92#include <unistd.h>
93#include <signal.h>
94#include <fcntl.h>
95#include <netdb.h>
96#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000097
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010098#define IS_EINTR( ret ) ( ( ret ) == EINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000099
Paul Bakkerdb20c102014-06-17 14:34:44 +0200100#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200102/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100103 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200104#if defined(_MSC_VER)
105#define MSVC_INT_CAST (int)
106#else
107#define MSVC_INT_CAST
108#endif
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112#include <time.h>
113
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200114#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000115
Paul Bakker5121ce52009-01-03 21:22:43 +0000116/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100117 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100119static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100121#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
122 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 WSADATA wsaData;
124
125 if( wsa_init_done == 0 )
126 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200127 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130 wsa_init_done = 1;
131 }
132#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100133#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 signal( SIGPIPE, SIG_IGN );
135#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100136#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100137 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100138}
139
140/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200141 * Initialize a context
142 */
143void mbedtls_net_init( mbedtls_net_context *ctx )
144{
145 ctx->fd = -1;
146}
147
148/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100149 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100150 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000151int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
152 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100153{
Janos Follath865b3eb2019-12-16 11:46:15 +0000154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100155 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100156
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100157 if( ( ret = net_prepare() ) != 0 )
158 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100159
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100160 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100161 memset( &hints, 0, sizeof( hints ) );
162 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
164 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100165
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200166 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100168
169 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100171 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
172 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200173 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100174 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200175 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100178 continue;
179 }
180
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200181 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100182 {
183 ret = 0;
184 break;
185 }
186
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200187 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100189 }
190
191 freeaddrinfo( addr_list );
192
193 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194}
195
196/*
197 * Create a listening socket on bind_ip:port
198 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200199int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000200{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100201 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100202 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100203
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100204 if( ( ret = net_prepare() ) != 0 )
205 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100206
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200207 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100208 memset( &hints, 0, sizeof( hints ) );
209 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
211 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100212 if( bind_ip == NULL )
213 hints.ai_flags = AI_PASSIVE;
214
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200215 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100217
218 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100220 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
221 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200222 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100223 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200224 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100227 continue;
228 }
229
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100230 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200231 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200232 (const char *) &n, sizeof( n ) ) != 0 )
233 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200234 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200236 continue;
237 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100238
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200239 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100240 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200241 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100243 continue;
244 }
245
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100246 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100248 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200249 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100250 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200251 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100253 continue;
254 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100255 }
256
Brian J Murray2adecba2016-11-06 04:45:15 -0800257 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100258 ret = 0;
259 break;
260 }
261
262 freeaddrinfo( addr_list );
263
264 return( ret );
265
Paul Bakker5121ce52009-01-03 21:22:43 +0000266}
267
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100268#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
269 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100270/*
271 * Check if the requested operation would be blocking on a non-blocking socket
272 * and thus 'failed' with a negative return value.
273 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200274static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100275{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200276 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100278}
Paul Bakker5121ce52009-01-03 21:22:43 +0000279#else
Paul Bakker80025412014-01-23 20:59:49 +0100280/*
281 * Check if the requested operation would be blocking on a non-blocking socket
282 * and thus 'failed' with a negative return value.
283 *
284 * Note: on a blocking socket this function always returns 0!
285 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200286static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100287{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100288 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100289
Paul Bakker80025412014-01-23 20:59:49 +0100290 /*
Jaeden Ameroa152e422019-05-29 09:38:29 +0100291 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100292 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200293 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100294 {
295 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100296 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100297 }
Paul Bakker80025412014-01-23 20:59:49 +0100298
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100299 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 {
301#if defined EAGAIN
302 case EAGAIN:
303#endif
304#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
305 case EWOULDBLOCK:
306#endif
307 return( 1 );
308 }
309 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200311#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313/*
314 * Accept a connection from a remote client
315 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200316int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
317 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200318 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000319{
Janos Follath865b3eb2019-12-16 11:46:15 +0000320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100321 int type;
322
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100323 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Paul Bakker394c56f2011-12-20 12:19:03 +0000325#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
nia0b01fd92020-06-11 12:29:15 +0100326 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
327 defined(socklen_t)
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100329 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330#else
331 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100332 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333#endif
334
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100335 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200336 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
337 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100338 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100341 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100343 if( type == SOCK_STREAM )
344 {
345 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200346 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000347 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100348 }
349 else
350 {
351 /* UDP: wait for a message, but keep it in the queue */
352 char buf[1] = { 0 };
353
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700354 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100355 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200356
357#if defined(_WIN32)
358 if( ret == SOCKET_ERROR &&
359 WSAGetLastError() == WSAEMSGSIZE )
360 {
361 /* We know buf is too small, thanks, just peeking here */
362 ret = 0;
363 }
364#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100365 }
366
367 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200369 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100370 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 }
374
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200375 /* UDP: hijack the listening socket to communicate with the client,
376 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100377 if( type != SOCK_STREAM )
378 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200379 struct sockaddr_storage local_addr;
380 int one = 1;
381
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200382 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100384
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200385 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200386 bind_ctx->fd = -1; /* In case we exit early */
387
388 n = sizeof( struct sockaddr_storage );
389 if( getsockname( client_ctx->fd,
390 (struct sockaddr *) &local_addr, &n ) != 0 ||
391 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
392 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
393 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
394 (const char *) &one, sizeof( one ) ) != 0 )
395 {
396 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
397 }
398
399 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
400 {
401 return( MBEDTLS_ERR_NET_BIND_FAILED );
402 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100403 }
404
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100406 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100407 if( client_addr.ss_family == AF_INET )
408 {
409 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200410 *ip_len = sizeof( addr4->sin_addr.s_addr );
411
412 if( buf_size < *ip_len )
413 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
414
415 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100416 }
417 else
418 {
419 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200420 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
421
422 if( buf_size < *ip_len )
423 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
424
425 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100426 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100427 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429 return( 0 );
430}
431
432/*
433 * Set the socket blocking or non-blocking
434 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200435int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000436{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100437#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
438 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000439 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200440 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200442 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443#endif
444}
445
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200446int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000447{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100448#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
449 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000450 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200451 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200453 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454#endif
455}
456
457/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100458 * Check if data is available on the socket
459 */
460
461int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
462{
Janos Follath865b3eb2019-12-16 11:46:15 +0000463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100464 struct timeval tv;
465
466 fd_set read_fds;
467 fd_set write_fds;
468
469 int fd = ctx->fd;
470
471 if( fd < 0 )
472 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
473
Gilles Peskineec4733b2018-04-05 14:55:47 +0200474#if defined(__has_feature)
475#if __has_feature(memory_sanitizer)
476 /* Ensure that memory sanitizers consider read_fds and write_fds as
477 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
478 * is implemented in assembly. */
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100479 memset( &read_fds, 0, sizeof( read_fds ) );
480 memset( &write_fds, 0, sizeof( write_fds ) );
Gilles Peskineec4733b2018-04-05 14:55:47 +0200481#endif
482#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100483
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100484 FD_ZERO( &read_fds );
485 if( rw & MBEDTLS_NET_POLL_READ )
486 {
487 rw &= ~MBEDTLS_NET_POLL_READ;
488 FD_SET( fd, &read_fds );
489 }
490
491 FD_ZERO( &write_fds );
492 if( rw & MBEDTLS_NET_POLL_WRITE )
493 {
494 rw &= ~MBEDTLS_NET_POLL_WRITE;
495 FD_SET( fd, &write_fds );
496 }
497
498 if( rw != 0 )
499 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
500
501 tv.tv_sec = timeout / 1000;
502 tv.tv_usec = ( timeout % 1000 ) * 1000;
503
Hanno Becker9ac64032018-03-15 12:19:31 +0000504 do
505 {
506 ret = select( fd + 1, &read_fds, &write_fds, NULL,
507 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000508 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100509 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100510
511 if( ret < 0 )
512 return( MBEDTLS_ERR_NET_POLL_FAILED );
513
514 ret = 0;
515 if( FD_ISSET( fd, &read_fds ) )
516 ret |= MBEDTLS_NET_POLL_READ;
517 if( FD_ISSET( fd, &write_fds ) )
518 ret |= MBEDTLS_NET_POLL_WRITE;
519
520 return( ret );
521}
522
523/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 * Portable usleep helper
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000527{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200528#if defined(_WIN32)
529 Sleep( ( usec + 999 ) / 1000 );
530#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100532 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200533#if defined(__unix__) || defined(__unix) || \
534 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100535 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200536#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100537 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200540#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000541}
542
543/*
544 * Read at most 'len' characters
545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100547{
Janos Follath865b3eb2019-12-16 11:46:15 +0000548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200549 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200550
551 if( fd < 0 )
552 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
553
554 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 if( ret < 0 )
557 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200558 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100559 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100561#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
562 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565#else
566 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100570 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571#endif
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 }
575
576 return( ret );
577}
578
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200579/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200580 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200581 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100582int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
583 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200584{
Janos Follath865b3eb2019-12-16 11:46:15 +0000585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200586 struct timeval tv;
587 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200588 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200589
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200590 if( fd < 0 )
591 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
592
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200593 FD_ZERO( &read_fds );
594 FD_SET( fd, &read_fds );
595
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200596 tv.tv_sec = timeout / 1000;
597 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200598
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200599 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200600
601 /* Zero fds ready means we timed out */
602 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100603 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200604
605 if( ret < 0 )
606 {
607#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
608 !defined(EFI32)
609 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100610 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200611#else
612 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100613 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200614#endif
615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200617 }
618
619 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200621}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200622
Paul Bakker5121ce52009-01-03 21:22:43 +0000623/*
624 * Write at most 'len' characters
625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000627{
Janos Follath865b3eb2019-12-16 11:46:15 +0000628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200629 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200630
631 if( fd < 0 )
632 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
633
634 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 if( ret < 0 )
637 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200638 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100639 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100641#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
642 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645#else
646 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100650 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000651#endif
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 }
655
656 return( ret );
657}
658
659/*
Robert Larsendf8e5112019-08-23 10:55:47 +0200660 * Close the connection
661 */
662void mbedtls_net_close( mbedtls_net_context *ctx )
663{
664 if( ctx->fd == -1 )
665 return;
666
667 close( ctx->fd );
668
669 ctx->fd = -1;
670}
671
672/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 * Gracefully close the connection
674 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200675void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000676{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200677 if( ctx->fd == -1 )
678 return;
679
680 shutdown( ctx->fd, 2 );
681 close( ctx->fd );
682
683 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000684}
685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#endif /* MBEDTLS_NET_C */