blob: 1e701a500036fa4d9012051821b502aee8a488ed [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 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Nicholas Wilson2682edf2017-12-05 12:08:15 +000047/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
48 * be set before config.h, which pulls in glibc's features.h indirectly.
49 * Harmless on other platforms. */
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070050#ifndef _POSIX_C_SOURCE
Nicholas Wilson2682edf2017-12-05 12:08:15 +000051#define _POSIX_C_SOURCE 200112L
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070052#endif
Nicholas Wilson2682edf2017-12-05 12:08:15 +000053
gufe4419735b62020-08-13 11:03:54 +020054#if defined(__NetBSD__)
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070055#ifndef _XOPEN_SOURCE
gufe4419735b62020-08-13 11:03:54 +020056#define _XOPEN_SOURCE 600 /* sockaddr_storage */
57#endif
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070058#endif
gufe4419735b62020-08-13 11:03:54 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020062#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010068#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040069 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
70 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010071#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
72#endif
73
SimonBd5800b72016-04-26 07:43:27 +010074#if defined(MBEDTLS_PLATFORM_C)
75#include "mbedtls/platform.h"
76#else
77#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010078#endif
79
Andres AG788aa4a2016-09-14 14:32:09 +010080#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Rich Evans00ab4702015-02-06 13:43:58 +000082#include <string.h>
83
Paul Bakkerfa6a6202013-10-28 18:48:30 +010084#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
85 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010087#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000088
Fabio Alessandrellidf608562018-04-03 19:40:11 +020089#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010090#undef _WIN32_WINNT
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010091/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010092#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020093#endif
94
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010095#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010096
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010097#include <winsock2.h>
98#include <windows.h>
99
Paul Bakkerf0fc2a22013-12-30 15:42:43 +0100100#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101#if defined(_WIN32_WCE)
102#pragma comment( lib, "ws2.lib" )
103#else
104#pragma comment( lib, "ws2_32.lib" )
105#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +0100106#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100108#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
109#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110#define close(fd) closesocket(fd)
111
112static int wsa_init_done = 0;
113
Paul Bakkerdb20c102014-06-17 14:34:44 +0200114#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116#include <sys/types.h>
117#include <sys/socket.h>
118#include <netinet/in.h>
119#include <arpa/inet.h>
120#include <sys/time.h>
121#include <unistd.h>
122#include <signal.h>
123#include <fcntl.h>
124#include <netdb.h>
125#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000126
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100127#define IS_EINTR( ret ) ( ( ret ) == EINTR )
Hanno Beckeref527962018-03-15 15:49:24 +0000128
Paul Bakkerdb20c102014-06-17 14:34:44 +0200129#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200131/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100132 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200133#if defined(_MSC_VER)
134#define MSVC_INT_CAST (int)
135#else
136#define MSVC_INT_CAST
137#endif
138
Paul Bakker5121ce52009-01-03 21:22:43 +0000139#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141#include <time.h>
142
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200143#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000144
Paul Bakker5121ce52009-01-03 21:22:43 +0000145/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100146 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100148static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100150#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
151 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 WSADATA wsaData;
153
154 if( wsa_init_done == 0 )
155 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200156 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 wsa_init_done = 1;
160 }
161#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100162#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 signal( SIGPIPE, SIG_IGN );
164#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100165#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100166 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100167}
168
169/*
Gilles Peskine9065d782021-06-20 23:08:19 +0200170 * Return 0 if the file descriptor is valid, an error otherwise.
171 * If for_select != 0, check whether the file descriptor is within the range
172 * allowed for fd_set used for the FD_xxx macros and the select() function.
173 */
174static int check_fd( int fd, int for_select )
175{
176 if( fd < 0 )
177 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
178
Gilles Peskine82976572021-06-20 22:01:36 +0200179#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
180 !defined(EFI32)
181 (void) for_select;
182#else
Gilles Peskine9065d782021-06-20 23:08:19 +0200183 /* A limitation of select() is that it only works with file descriptors
184 * that are strictly less than FD_SETSIZE. This is a limitation of the
185 * fd_set type. Error out early, because attempting to call FD_SET on a
186 * large file descriptor is a buffer overflow on typical platforms. */
187 if( for_select && fd >= FD_SETSIZE )
188 return( MBEDTLS_ERR_NET_POLL_FAILED );
Gilles Peskine82976572021-06-20 22:01:36 +0200189#endif
Gilles Peskine9065d782021-06-20 23:08:19 +0200190
191 return( 0 );
192}
193
194/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200195 * Initialize a context
196 */
197void mbedtls_net_init( mbedtls_net_context *ctx )
198{
199 ctx->fd = -1;
200}
201
202/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100203 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100204 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000205int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
206 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100207{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100208 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100209 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100210
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100211 if( ( ret = net_prepare() ) != 0 )
212 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100213
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100214 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100215 memset( &hints, 0, sizeof( hints ) );
216 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
218 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100219
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200220 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100222
223 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100225 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
226 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200227 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100228 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200229 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100232 continue;
233 }
234
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200235 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100236 {
237 ret = 0;
238 break;
239 }
240
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_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100243 }
244
245 freeaddrinfo( addr_list );
246
247 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248}
249
250/*
251 * Create a listening socket on bind_ip:port
252 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200253int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000254{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100255 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100256 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100257
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100258 if( ( ret = net_prepare() ) != 0 )
259 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100260
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200261 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100262 memset( &hints, 0, sizeof( hints ) );
263 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
265 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100266 if( bind_ip == NULL )
267 hints.ai_flags = AI_PASSIVE;
268
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200269 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100271
272 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100274 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
275 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200276 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100277 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200278 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100281 continue;
282 }
283
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100284 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200285 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200286 (const char *) &n, sizeof( n ) ) != 0 )
287 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200288 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200290 continue;
291 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100292
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200293 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100294 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200295 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100297 continue;
298 }
299
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100300 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100302 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200303 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100304 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200305 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100307 continue;
308 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100309 }
310
Brian J Murray2adecba2016-11-06 04:45:15 -0800311 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100312 ret = 0;
313 break;
314 }
315
316 freeaddrinfo( addr_list );
317
318 return( ret );
319
Paul Bakker5121ce52009-01-03 21:22:43 +0000320}
321
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100322#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
323 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100324/*
325 * Check if the requested operation would be blocking on a non-blocking socket
326 * and thus 'failed' with a negative return value.
327 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200328static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100329{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200330 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100332}
Paul Bakker5121ce52009-01-03 21:22:43 +0000333#else
Paul Bakker80025412014-01-23 20:59:49 +0100334/*
335 * Check if the requested operation would be blocking on a non-blocking socket
336 * and thus 'failed' with a negative return value.
337 *
338 * Note: on a blocking socket this function always returns 0!
339 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200340static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100341{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100342 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100343
Paul Bakker80025412014-01-23 20:59:49 +0100344 /*
Jaeden Amero88a6e892019-05-29 09:38:29 +0100345 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100346 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200347 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100348 {
349 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100350 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100351 }
Paul Bakker80025412014-01-23 20:59:49 +0100352
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100353 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 {
355#if defined EAGAIN
356 case EAGAIN:
357#endif
358#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
359 case EWOULDBLOCK:
360#endif
361 return( 1 );
362 }
363 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200365#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367/*
368 * Accept a connection from a remote client
369 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200370int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
371 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200372 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000373{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100374 int ret;
375 int type;
376
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100377 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
gufe4419735b62020-08-13 11:03:54 +0200379#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
380 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
381 ( defined(__NetBSD__) && defined(socklen_t) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100383 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384#else
385 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100386 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387#endif
388
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100389 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200390 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
391 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100392 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100395 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000396
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100397 if( type == SOCK_STREAM )
398 {
399 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200400 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000401 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100402 }
403 else
404 {
405 /* UDP: wait for a message, but keep it in the queue */
406 char buf[1] = { 0 };
407
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700408 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100409 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200410
411#if defined(_WIN32)
412 if( ret == SOCKET_ERROR &&
413 WSAGetLastError() == WSAEMSGSIZE )
414 {
415 /* We know buf is too small, thanks, just peeking here */
416 ret = 0;
417 }
418#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100419 }
420
421 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200423 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100424 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 }
428
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200429 /* UDP: hijack the listening socket to communicate with the client,
430 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100431 if( type != SOCK_STREAM )
432 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200433 struct sockaddr_storage local_addr;
434 int one = 1;
435
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200436 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100438
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200439 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200440 bind_ctx->fd = -1; /* In case we exit early */
441
442 n = sizeof( struct sockaddr_storage );
443 if( getsockname( client_ctx->fd,
444 (struct sockaddr *) &local_addr, &n ) != 0 ||
445 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
446 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
447 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
448 (const char *) &one, sizeof( one ) ) != 0 )
449 {
450 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
451 }
452
453 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
454 {
455 return( MBEDTLS_ERR_NET_BIND_FAILED );
456 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100457 }
458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100460 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100461 if( client_addr.ss_family == AF_INET )
462 {
463 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200464 *ip_len = sizeof( addr4->sin_addr.s_addr );
465
466 if( buf_size < *ip_len )
467 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
468
469 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100470 }
471 else
472 {
473 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200474 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
475
476 if( buf_size < *ip_len )
477 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
478
479 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100480 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100481 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483 return( 0 );
484}
485
486/*
487 * Set the socket blocking or non-blocking
488 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200489int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000490{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100491#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
492 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000493 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200494 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200496 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497#endif
498}
499
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200500int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000501{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100502#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
503 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000504 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200505 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200507 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508#endif
509}
510
511/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100512 * Check if data is available on the socket
513 */
514
515int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
516{
517 int ret;
518 struct timeval tv;
519
520 fd_set read_fds;
521 fd_set write_fds;
522
523 int fd = ctx->fd;
524
Gilles Peskine9065d782021-06-20 23:08:19 +0200525 ret = check_fd( fd, 1 );
526 if( ret != 0 )
527 return( ret );
Gilles Peskinef6042402021-02-24 19:49:44 +0100528
Gilles Peskineec4733b2018-04-05 14:55:47 +0200529#if defined(__has_feature)
530#if __has_feature(memory_sanitizer)
531 /* Ensure that memory sanitizers consider read_fds and write_fds as
532 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
533 * is implemented in assembly. */
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100534 memset( &read_fds, 0, sizeof( read_fds ) );
535 memset( &write_fds, 0, sizeof( write_fds ) );
Gilles Peskineec4733b2018-04-05 14:55:47 +0200536#endif
537#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100538
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100539 FD_ZERO( &read_fds );
540 if( rw & MBEDTLS_NET_POLL_READ )
541 {
542 rw &= ~MBEDTLS_NET_POLL_READ;
543 FD_SET( fd, &read_fds );
544 }
545
546 FD_ZERO( &write_fds );
547 if( rw & MBEDTLS_NET_POLL_WRITE )
548 {
549 rw &= ~MBEDTLS_NET_POLL_WRITE;
550 FD_SET( fd, &write_fds );
551 }
552
553 if( rw != 0 )
554 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
555
556 tv.tv_sec = timeout / 1000;
557 tv.tv_usec = ( timeout % 1000 ) * 1000;
558
Hanno Becker9ac64032018-03-15 12:19:31 +0000559 do
560 {
561 ret = select( fd + 1, &read_fds, &write_fds, NULL,
562 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000563 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100564 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100565
566 if( ret < 0 )
567 return( MBEDTLS_ERR_NET_POLL_FAILED );
568
569 ret = 0;
570 if( FD_ISSET( fd, &read_fds ) )
571 ret |= MBEDTLS_NET_POLL_READ;
572 if( FD_ISSET( fd, &write_fds ) )
573 ret |= MBEDTLS_NET_POLL_WRITE;
574
575 return( ret );
576}
577
578/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 * Portable usleep helper
580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000582{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200583#if defined(_WIN32)
584 Sleep( ( usec + 999 ) / 1000 );
585#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100587 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200588#if defined(__unix__) || defined(__unix) || \
589 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100590 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200591#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100592 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200593#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200595#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000596}
597
598/*
599 * Read at most 'len' characters
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100602{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200603 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200604 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200605
Gilles Peskine9065d782021-06-20 23:08:19 +0200606 ret = check_fd( fd, 0 );
607 if( ret != 0 )
608 return( ret );
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200609
610 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 if( ret < 0 )
613 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200614 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100615 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100617#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
618 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621#else
622 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100626 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627#endif
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
632 return( ret );
633}
634
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200635/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200636 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200637 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100638int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
639 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200640{
641 int ret;
642 struct timeval tv;
643 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200644 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200645
Gilles Peskine9065d782021-06-20 23:08:19 +0200646 ret = check_fd( fd, 1 );
647 if( ret != 0 )
648 return( ret );
Gilles Peskinef6042402021-02-24 19:49:44 +0100649
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200650 FD_ZERO( &read_fds );
651 FD_SET( fd, &read_fds );
652
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200653 tv.tv_sec = timeout / 1000;
654 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200655
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200656 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200657
658 /* Zero fds ready means we timed out */
659 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100660 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200661
662 if( ret < 0 )
663 {
664#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
665 !defined(EFI32)
666 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100667 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200668#else
669 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100670 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200671#endif
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200674 }
675
676 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200678}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200679
Paul Bakker5121ce52009-01-03 21:22:43 +0000680/*
681 * Write at most 'len' characters
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000684{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200685 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200686 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200687
Gilles Peskine9065d782021-06-20 23:08:19 +0200688 ret = check_fd( fd, 0 );
689 if( ret != 0 )
690 return( ret );
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200691
692 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
694 if( ret < 0 )
695 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200696 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100697 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100699#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
700 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703#else
704 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100708 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709#endif
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 }
713
714 return( ret );
715}
716
717/*
718 * Gracefully close the connection
719 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200720void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000721{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200722 if( ctx->fd == -1 )
723 return;
724
725 shutdown( ctx->fd, 2 );
726 close( ctx->fd );
727
728 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000729}
730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#endif /* MBEDTLS_NET_C */