blob: 1a88c223d43daeebaa6641439d05d4f67571fd57 [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
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010035#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040036 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
37 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010038#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
39#endif
40
SimonBd5800b72016-04-26 07:43:27 +010041#if defined(MBEDTLS_PLATFORM_C)
42#include "mbedtls/platform.h"
43#else
44#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010045#endif
46
Andres AG788aa4a2016-09-14 14:32:09 +010047#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <string.h>
50
Janos Follath865b3eb2019-12-16 11:46:15 +000051#include "mbedtls/error.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
Fabio Alessandrellidf608562018-04-03 19:40:11 +020058#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010059#undef _WIN32_WINNT
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010060/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010061#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020062#endif
63
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010064#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010065
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010066#include <winsock2.h>
67#include <windows.h>
68
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010069#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000070#if defined(_WIN32_WCE)
71#pragma comment( lib, "ws2.lib" )
72#else
73#pragma comment( lib, "ws2_32.lib" )
74#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010075#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010077#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
78#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000079#define close(fd) closesocket(fd)
80
81static int wsa_init_done = 0;
82
Paul Bakkerdb20c102014-06-17 14:34:44 +020083#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85#include <sys/types.h>
86#include <sys/socket.h>
87#include <netinet/in.h>
88#include <arpa/inet.h>
89#include <sys/time.h>
90#include <unistd.h>
91#include <signal.h>
92#include <fcntl.h>
93#include <netdb.h>
94#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000095
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010096#define IS_EINTR( ret ) ( ( ret ) == EINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000097
Paul Bakkerdb20c102014-06-17 14:34:44 +020098#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200100/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100101 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200102#if defined(_MSC_VER)
103#define MSVC_INT_CAST (int)
104#else
105#define MSVC_INT_CAST
106#endif
107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110#include <time.h>
111
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200112#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100115 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100117static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100119#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
120 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 WSADATA wsaData;
122
123 if( wsa_init_done == 0 )
124 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200125 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128 wsa_init_done = 1;
129 }
130#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100131#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 signal( SIGPIPE, SIG_IGN );
133#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100134#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100135 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100136}
137
138/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200139 * Initialize a context
140 */
141void mbedtls_net_init( mbedtls_net_context *ctx )
142{
143 ctx->fd = -1;
144}
145
146/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100147 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100148 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000149int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
150 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100151{
Janos Follath865b3eb2019-12-16 11:46:15 +0000152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100153 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100154
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100155 if( ( ret = net_prepare() ) != 0 )
156 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100157
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100158 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100159 memset( &hints, 0, sizeof( hints ) );
160 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
162 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100163
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200164 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100166
167 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100169 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
170 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200171 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100172 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200173 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100176 continue;
177 }
178
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200179 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100180 {
181 ret = 0;
182 break;
183 }
184
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200185 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187 }
188
189 freeaddrinfo( addr_list );
190
191 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192}
193
194/*
195 * Create a listening socket on bind_ip:port
196 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200197int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100199 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100200 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100201
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100202 if( ( ret = net_prepare() ) != 0 )
203 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100204
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200205 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100206 memset( &hints, 0, sizeof( hints ) );
207 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
209 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100210 if( bind_ip == NULL )
211 hints.ai_flags = AI_PASSIVE;
212
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200213 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100215
216 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100218 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
219 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200220 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100221 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200222 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100225 continue;
226 }
227
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100228 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200229 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200230 (const char *) &n, sizeof( n ) ) != 0 )
231 {
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_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200234 continue;
235 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100236
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200237 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100238 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200239 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100241 continue;
242 }
243
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100244 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100246 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200247 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100248 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200249 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100251 continue;
252 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100253 }
254
Brian J Murray2adecba2016-11-06 04:45:15 -0800255 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100256 ret = 0;
257 break;
258 }
259
260 freeaddrinfo( addr_list );
261
262 return( ret );
263
Paul Bakker5121ce52009-01-03 21:22:43 +0000264}
265
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100266#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
267 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100268/*
269 * Check if the requested operation would be blocking on a non-blocking socket
270 * and thus 'failed' with a negative return value.
271 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200272static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100273{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200274 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100276}
Paul Bakker5121ce52009-01-03 21:22:43 +0000277#else
Paul Bakker80025412014-01-23 20:59:49 +0100278/*
279 * Check if the requested operation would be blocking on a non-blocking socket
280 * and thus 'failed' with a negative return value.
281 *
282 * Note: on a blocking socket this function always returns 0!
283 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200284static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100285{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100286 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100287
Paul Bakker80025412014-01-23 20:59:49 +0100288 /*
Jaeden Ameroa152e422019-05-29 09:38:29 +0100289 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100290 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200291 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100292 {
293 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100294 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100295 }
Paul Bakker80025412014-01-23 20:59:49 +0100296
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100297 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 {
299#if defined EAGAIN
300 case EAGAIN:
301#endif
302#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
303 case EWOULDBLOCK:
304#endif
305 return( 1 );
306 }
307 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200309#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311/*
312 * Accept a connection from a remote client
313 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200314int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
315 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200316 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000317{
Janos Follath865b3eb2019-12-16 11:46:15 +0000318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100319 int type;
320
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100321 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Paul Bakker394c56f2011-12-20 12:19:03 +0000323#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
Manuel Pégourié-Gonnard04317352015-10-05 12:16:06 +0100324 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100326 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327#else
328 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100329 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330#endif
331
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100332 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200333 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
334 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100335 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100338 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100340 if( type == SOCK_STREAM )
341 {
342 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200343 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000344 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100345 }
346 else
347 {
348 /* UDP: wait for a message, but keep it in the queue */
349 char buf[1] = { 0 };
350
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700351 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100352 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200353
354#if defined(_WIN32)
355 if( ret == SOCKET_ERROR &&
356 WSAGetLastError() == WSAEMSGSIZE )
357 {
358 /* We know buf is too small, thanks, just peeking here */
359 ret = 0;
360 }
361#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100362 }
363
364 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200366 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100367 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 }
371
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200372 /* UDP: hijack the listening socket to communicate with the client,
373 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100374 if( type != SOCK_STREAM )
375 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200376 struct sockaddr_storage local_addr;
377 int one = 1;
378
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200379 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100381
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200382 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200383 bind_ctx->fd = -1; /* In case we exit early */
384
385 n = sizeof( struct sockaddr_storage );
386 if( getsockname( client_ctx->fd,
387 (struct sockaddr *) &local_addr, &n ) != 0 ||
388 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
389 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
390 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
391 (const char *) &one, sizeof( one ) ) != 0 )
392 {
393 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
394 }
395
396 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
397 {
398 return( MBEDTLS_ERR_NET_BIND_FAILED );
399 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100400 }
401
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100403 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100404 if( client_addr.ss_family == AF_INET )
405 {
406 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200407 *ip_len = sizeof( addr4->sin_addr.s_addr );
408
409 if( buf_size < *ip_len )
410 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
411
412 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100413 }
414 else
415 {
416 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200417 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
418
419 if( buf_size < *ip_len )
420 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
421
422 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100423 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100424 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426 return( 0 );
427}
428
429/*
430 * Set the socket blocking or non-blocking
431 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200432int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000433{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100434#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
435 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000436 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200437 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200439 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440#endif
441}
442
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200443int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000444{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100445#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
446 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000447 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200448 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200450 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451#endif
452}
453
454/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100455 * Check if data is available on the socket
456 */
457
458int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
459{
Janos Follath865b3eb2019-12-16 11:46:15 +0000460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100461 struct timeval tv;
462
463 fd_set read_fds;
464 fd_set write_fds;
465
466 int fd = ctx->fd;
467
468 if( fd < 0 )
469 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
470
Gilles Peskineec4733b2018-04-05 14:55:47 +0200471#if defined(__has_feature)
472#if __has_feature(memory_sanitizer)
473 /* Ensure that memory sanitizers consider read_fds and write_fds as
474 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
475 * is implemented in assembly. */
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100476 memset( &read_fds, 0, sizeof( read_fds ) );
477 memset( &write_fds, 0, sizeof( write_fds ) );
Gilles Peskineec4733b2018-04-05 14:55:47 +0200478#endif
479#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100480
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100481 FD_ZERO( &read_fds );
482 if( rw & MBEDTLS_NET_POLL_READ )
483 {
484 rw &= ~MBEDTLS_NET_POLL_READ;
485 FD_SET( fd, &read_fds );
486 }
487
488 FD_ZERO( &write_fds );
489 if( rw & MBEDTLS_NET_POLL_WRITE )
490 {
491 rw &= ~MBEDTLS_NET_POLL_WRITE;
492 FD_SET( fd, &write_fds );
493 }
494
495 if( rw != 0 )
496 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
497
498 tv.tv_sec = timeout / 1000;
499 tv.tv_usec = ( timeout % 1000 ) * 1000;
500
Hanno Becker9ac64032018-03-15 12:19:31 +0000501 do
502 {
503 ret = select( fd + 1, &read_fds, &write_fds, NULL,
504 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000505 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100506 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100507
508 if( ret < 0 )
509 return( MBEDTLS_ERR_NET_POLL_FAILED );
510
511 ret = 0;
512 if( FD_ISSET( fd, &read_fds ) )
513 ret |= MBEDTLS_NET_POLL_READ;
514 if( FD_ISSET( fd, &write_fds ) )
515 ret |= MBEDTLS_NET_POLL_WRITE;
516
517 return( ret );
518}
519
520/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 * Portable usleep helper
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000524{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200525#if defined(_WIN32)
526 Sleep( ( usec + 999 ) / 1000 );
527#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100529 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200530#if defined(__unix__) || defined(__unix) || \
531 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100532 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200533#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100534 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200535#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200537#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000538}
539
540/*
541 * Read at most 'len' characters
542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100544{
Janos Follath865b3eb2019-12-16 11:46:15 +0000545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200546 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200547
548 if( fd < 0 )
549 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
550
551 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 if( ret < 0 )
554 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200555 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100556 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100558#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
559 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000562#else
563 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100567 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568#endif
569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 }
572
573 return( ret );
574}
575
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200576/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200577 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200578 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100579int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
580 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200581{
Janos Follath865b3eb2019-12-16 11:46:15 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200583 struct timeval tv;
584 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200585 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200586
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200587 if( fd < 0 )
588 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
589
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200590 FD_ZERO( &read_fds );
591 FD_SET( fd, &read_fds );
592
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200593 tv.tv_sec = timeout / 1000;
594 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200595
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200596 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200597
598 /* Zero fds ready means we timed out */
599 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100600 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200601
602 if( ret < 0 )
603 {
604#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
605 !defined(EFI32)
606 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100607 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200608#else
609 if( errno == EINTR )
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#endif
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200614 }
615
616 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200618}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200619
Paul Bakker5121ce52009-01-03 21:22:43 +0000620/*
621 * Write at most 'len' characters
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000624{
Janos Follath865b3eb2019-12-16 11:46:15 +0000625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200626 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200627
628 if( fd < 0 )
629 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
630
631 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632
633 if( ret < 0 )
634 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200635 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100636 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100638#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
639 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642#else
643 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100647 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648#endif
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 }
652
653 return( ret );
654}
655
656/*
Robert Larsendf8e5112019-08-23 10:55:47 +0200657 * Close the connection
658 */
659void mbedtls_net_close( mbedtls_net_context *ctx )
660{
661 if( ctx->fd == -1 )
662 return;
663
664 close( ctx->fd );
665
666 ctx->fd = -1;
667}
668
669/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 * Gracefully close the connection
671 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200672void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000673{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200674 if( ctx->fd == -1 )
675 return;
676
677 shutdown( ctx->fd, 2 );
678 close( ctx->fd );
679
680 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000681}
682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_NET_C */