blob: 2c2a876b022d755d248bf963ce6cd06669dcf5f7 [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úti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Nicholas Wilson2682edf2017-12-05 12:08:15 +000020/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
21 * be set before config.h, which pulls in glibc's features.h indirectly.
22 * Harmless on other platforms. */
Flavio Ceolina79c30b2020-05-27 22:41:19 -070023#ifndef _POSIX_C_SOURCE
Nicholas Wilson2682edf2017-12-05 12:08:15 +000024#define _POSIX_C_SOURCE 200112L
Flavio Ceolina79c30b2020-05-27 22:41:19 -070025#endif
26#ifndef _XOPEN_SOURCE
nia0b01fd92020-06-11 12:29:15 +010027#define _XOPEN_SOURCE 600 /* sockaddr_storage */
Flavio Ceolina79c30b2020-05-27 22:41:19 -070028#endif
Nicholas Wilson2682edf2017-12-05 12:08:15 +000029
Gilles Peskinedb09ef62020-06-03 01:43:33 +020030#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010034#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040035 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000036 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010037#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
38#endif
39
SimonBd5800b72016-04-26 07:43:27 +010040#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010041
Andres AG788aa4a2016-09-14 14:32:09 +010042#include "mbedtls/net_sockets.h"
Janos Follath73c616b2019-12-18 15:07:04 +000043#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Paul Bakkerfa6a6202013-10-28 18:48:30 +010047#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
48 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050#define IS_EINTR(ret) ((ret) == WSAEINTR)
Hanno Beckeref527962018-03-15 15:49:24 +000051
opatomic2b2acf12020-04-24 10:00:36 -040052#if !defined(_WIN32_WINNT)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010053/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010054#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020055#endif
56
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010057#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010058
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010059#include <winsock2.h>
60#include <windows.h>
opatomic2b2acf12020-04-24 10:00:36 -040061#if (_WIN32_WINNT < 0x0501)
62#include <wspiapi.h>
63#endif
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010064
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010065#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000066#if defined(_WIN32_WCE)
67#pragma comment( lib, "ws2.lib" )
68#else
69#pragma comment( lib, "ws2_32.lib" )
70#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010071#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073#define read(fd, buf, len) recv(fd, (char *) (buf), (int) (len), 0)
74#define write(fd, buf, len) send(fd, (char *) (buf), (int) (len), 0)
Paul Bakker5121ce52009-01-03 21:22:43 +000075#define close(fd) closesocket(fd)
76
77static int wsa_init_done = 0;
78
Paul Bakkerdb20c102014-06-17 14:34:44 +020079#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81#include <sys/types.h>
82#include <sys/socket.h>
83#include <netinet/in.h>
84#include <arpa/inet.h>
85#include <sys/time.h>
86#include <unistd.h>
87#include <signal.h>
88#include <fcntl.h>
89#include <netdb.h>
90#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092#define IS_EINTR(ret) ((ret) == EINTR)
Sergey20003ca2023-03-06 16:01:21 -070093#define SOCKET int
Hanno Beckeref527962018-03-15 15:49:24 +000094
Paul Bakkerdb20c102014-06-17 14:34:44 +020095#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020097/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010098 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020099#if defined(_MSC_VER)
100#define MSVC_INT_CAST (int)
101#else
102#define MSVC_INT_CAST
103#endif
104
Paul Bakker5121ce52009-01-03 21:22:43 +0000105#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200106
Andrzej Kurek516e1b02022-03-02 10:55:08 -0500107#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000108#include <time.h>
Andrzej Kurek516e1b02022-03-02 10:55:08 -0500109#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200111#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100114 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116static int net_prepare(void)
Paul Bakker5121ce52009-01-03 21:22:43 +0000117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100119 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 WSADATA wsaData;
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if (wsa_init_done == 0) {
123 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
124 return MBEDTLS_ERR_NET_SOCKET_FAILED;
125 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 wsa_init_done = 1;
128 }
129#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100130#if !defined(EFIX64) && !defined(EFI32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 signal(SIGPIPE, SIG_IGN);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100133#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 return 0;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100135}
136
137/*
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200138 * Return 0 if the file descriptor is valid, an error otherwise.
139 * If for_select != 0, check whether the file descriptor is within the range
140 * allowed for fd_set used for the FD_xxx macros and the select() function.
141 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142static int check_fd(int fd, int for_select)
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200143{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (fd < 0) {
145 return MBEDTLS_ERR_NET_INVALID_CONTEXT;
146 }
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200147
Gilles Peskine51859aa2021-06-20 22:01:36 +0200148#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
149 !defined(EFI32)
150 (void) for_select;
151#else
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200152 /* A limitation of select() is that it only works with file descriptors
153 * that are strictly less than FD_SETSIZE. This is a limitation of the
154 * fd_set type. Error out early, because attempting to call FD_SET on a
155 * large file descriptor is a buffer overflow on typical platforms. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (for_select && fd >= FD_SETSIZE) {
157 return MBEDTLS_ERR_NET_POLL_FAILED;
158 }
Gilles Peskine51859aa2021-06-20 22:01:36 +0200159#endif
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 return 0;
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200162}
163
164/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200165 * Initialize a context
166 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167void mbedtls_net_init(mbedtls_net_context *ctx)
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200168{
169 ctx->fd = -1;
170}
171
172/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100173 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100174 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
176 const char *port, int proto)
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100177{
Janos Follath865b3eb2019-12-16 11:46:15 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100179 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 if ((ret = net_prepare()) != 0) {
182 return ret;
183 }
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100184
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100185 /* Do name resolution with both IPv6 and IPv4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 memset(&hints, 0, sizeof(hints));
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
189 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
192 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
193 }
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100194
195 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
198 ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
199 cur->ai_protocol);
200 if (ctx->fd < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100202 continue;
203 }
204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (connect(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) == 0) {
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100206 ret = 0;
207 break;
208 }
209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100212 }
213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 freeaddrinfo(addr_list);
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217}
218
219/*
220 * Create a listening socket on bind_ip:port
221 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto)
Paul Bakker5121ce52009-01-03 21:22:43 +0000223{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100224 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100225 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 if ((ret = net_prepare()) != 0) {
228 return ret;
229 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100230
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200231 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 memset(&hints, 0, sizeof(hints));
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100233 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
235 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if (bind_ip == NULL) {
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100237 hints.ai_flags = AI_PASSIVE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if (getaddrinfo(bind_ip, port, &hints, &addr_list) != 0) {
241 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
242 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100243
244 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
247 ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
248 cur->ai_protocol);
249 if (ctx->fd < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100251 continue;
252 }
253
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100254 n = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
256 (const char *) &n, sizeof(n)) != 0) {
257 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200259 continue;
260 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100261
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
263 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100265 continue;
266 }
267
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100268 /* Listen only makes sense for TCP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (proto == MBEDTLS_NET_PROTO_TCP) {
270 if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
271 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100273 continue;
274 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100275 }
276
Brian J Murray2adecba2016-11-06 04:45:15 -0800277 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100278 ret = 0;
279 break;
280 }
281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 freeaddrinfo(addr_list);
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 return ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100285
Paul Bakker5121ce52009-01-03 21:22:43 +0000286}
287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100289 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100290/*
291 * Check if the requested operation would be blocking on a non-blocking socket
292 * and thus 'failed' with a negative return value.
293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294static int net_would_block(const mbedtls_net_context *ctx)
Paul Bakker80025412014-01-23 20:59:49 +0100295{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200296 ((void) ctx);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return WSAGetLastError() == WSAEWOULDBLOCK;
Paul Bakker80025412014-01-23 20:59:49 +0100298}
Paul Bakker5121ce52009-01-03 21:22:43 +0000299#else
Paul Bakker80025412014-01-23 20:59:49 +0100300/*
301 * Check if the requested operation would be blocking on a non-blocking socket
302 * and thus 'failed' with a negative return value.
303 *
304 * Note: on a blocking socket this function always returns 0!
305 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306static int net_would_block(const mbedtls_net_context *ctx)
Paul Bakker80025412014-01-23 20:59:49 +0100307{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100308 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100309
Paul Bakker80025412014-01-23 20:59:49 +0100310 /*
Jaeden Ameroa152e422019-05-29 09:38:29 +0100311 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100312 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if ((fcntl(ctx->fd, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100314 errno = err;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return 0;
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100316 }
Paul Bakker80025412014-01-23 20:59:49 +0100317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 switch (errno = err) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000319#if defined EAGAIN
320 case EAGAIN:
321#endif
322#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
323 case EWOULDBLOCK:
324#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 return 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000328}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200329#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/*
332 * Accept a connection from a remote client
333 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
335 mbedtls_net_context *client_ctx,
336 void *client_ip, size_t buf_size, size_t *ip_len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000337{
Janos Follath865b3eb2019-12-16 11:46:15 +0000338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100339 int type;
340
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100341 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Paul Bakker394c56f2011-12-20 12:19:03 +0000343#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
nia0b01fd92020-06-11 12:29:15 +0100344 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
okhowang(王沛文)76158ce2020-09-03 15:36:36 +0800345 defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 socklen_t n = (socklen_t) sizeof(client_addr);
347 socklen_t type_len = (socklen_t) sizeof(type);
Paul Bakker5121ce52009-01-03 21:22:43 +0000348#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 int n = (int) sizeof(client_addr);
350 int type_len = (int) sizeof(type);
Paul Bakker5121ce52009-01-03 21:22:43 +0000351#endif
352
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100353 /* Is this a TCP or UDP socket? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE,
355 (void *) &type, &type_len) != 0 ||
356 (type != SOCK_STREAM && type != SOCK_DGRAM)) {
357 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100358 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (type == SOCK_STREAM) {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100361 /* TCP: actual accept() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 ret = client_ctx->fd = (int) accept(bind_ctx->fd,
363 (struct sockaddr *) &client_addr, &n);
364 } else {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100365 /* UDP: wait for a message, but keep it in the queue */
366 char buf[1] = { 0 };
367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 ret = (int) recvfrom(bind_ctx->fd, buf, sizeof(buf), MSG_PEEK,
369 (struct sockaddr *) &client_addr, &n);
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200370
371#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 if (ret == SOCKET_ERROR &&
373 WSAGetLastError() == WSAEMSGSIZE) {
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200374 /* We know buf is too small, thanks, just peeking here */
375 ret = 0;
376 }
377#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100378 }
379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (ret < 0) {
381 if (net_would_block(bind_ctx) != 0) {
382 return MBEDTLS_ERR_SSL_WANT_READ;
383 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 }
387
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200388 /* UDP: hijack the listening socket to communicate with the client,
389 * then bind a new socket to accept new connections */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 if (type != SOCK_STREAM) {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200391 struct sockaddr_storage local_addr;
392 int one = 1;
393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 if (connect(bind_ctx->fd, (struct sockaddr *) &client_addr, n) != 0) {
395 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
396 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100397
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200398 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200399 bind_ctx->fd = -1; /* In case we exit early */
400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 n = sizeof(struct sockaddr_storage);
402 if (getsockname(client_ctx->fd,
403 (struct sockaddr *) &local_addr, &n) != 0 ||
404 (bind_ctx->fd = (int) socket(local_addr.ss_family,
405 SOCK_DGRAM, IPPROTO_UDP)) < 0 ||
406 setsockopt(bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
407 (const char *) &one, sizeof(one)) != 0) {
408 return MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200409 }
410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 if (bind(bind_ctx->fd, (struct sockaddr *) &local_addr, n) != 0) {
412 return MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200413 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100414 }
415
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 if (client_ip != NULL) {
417 if (client_addr.ss_family == AF_INET) {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100418 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 *ip_len = sizeof(addr4->sin_addr.s_addr);
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 if (buf_size < *ip_len) {
422 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
423 }
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 memcpy(client_ip, &addr4->sin_addr.s_addr, *ip_len);
426 } else {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100427 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 *ip_len = sizeof(addr6->sin6_addr.s6_addr);
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (buf_size < *ip_len) {
431 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
432 }
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200433
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 memcpy(client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100435 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100436 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000439}
440
441/*
442 * Set the socket blocking or non-blocking
443 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444int mbedtls_net_set_block(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000445{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100447 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000448 u_long n = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 return ioctlsocket(ctx->fd, FIONBIO, &n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000450#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) & ~O_NONBLOCK);
Paul Bakker5121ce52009-01-03 21:22:43 +0000452#endif
453}
454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000456{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100458 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000459 u_long n = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 return ioctlsocket(ctx->fd, FIONBIO, &n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000461#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) | O_NONBLOCK);
Paul Bakker5121ce52009-01-03 21:22:43 +0000463#endif
464}
465
466/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100467 * Check if data is available on the socket
468 */
469
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100471{
Janos Follath865b3eb2019-12-16 11:46:15 +0000472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100473 struct timeval tv;
474
475 fd_set read_fds;
476 fd_set write_fds;
477
478 int fd = ctx->fd;
479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 ret = check_fd(fd, 1);
481 if (ret != 0) {
482 return ret;
483 }
Gilles Peskineddf43742021-02-24 19:49:44 +0100484
Gilles Peskineec4733b2018-04-05 14:55:47 +0200485#if defined(__has_feature)
486#if __has_feature(memory_sanitizer)
487 /* Ensure that memory sanitizers consider read_fds and write_fds as
488 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
489 * is implemented in assembly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 memset(&read_fds, 0, sizeof(read_fds));
491 memset(&write_fds, 0, sizeof(write_fds));
Gilles Peskineec4733b2018-04-05 14:55:47 +0200492#endif
493#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 FD_ZERO(&read_fds);
496 if (rw & MBEDTLS_NET_POLL_READ) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100497 rw &= ~MBEDTLS_NET_POLL_READ;
Sergey20003ca2023-03-06 16:01:21 -0700498 FD_SET((SOCKET) fd, &read_fds);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100499 }
500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 FD_ZERO(&write_fds);
502 if (rw & MBEDTLS_NET_POLL_WRITE) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100503 rw &= ~MBEDTLS_NET_POLL_WRITE;
Sergey20003ca2023-03-06 16:01:21 -0700504 FD_SET((SOCKET) fd, &write_fds);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100505 }
506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (rw != 0) {
508 return MBEDTLS_ERR_NET_BAD_INPUT_DATA;
509 }
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100510
511 tv.tv_sec = timeout / 1000;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 tv.tv_usec = (timeout % 1000) * 1000;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 do {
515 ret = select(fd + 1, &read_fds, &write_fds, NULL,
516 timeout == (uint32_t) -1 ? NULL : &tv);
517 } while (IS_EINTR(ret));
518
519 if (ret < 0) {
520 return MBEDTLS_ERR_NET_POLL_FAILED;
Hanno Becker80e06d72018-03-15 14:41:55 +0000521 }
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100522
523 ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 if (FD_ISSET(fd, &read_fds)) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100525 ret |= MBEDTLS_NET_POLL_READ;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 }
527 if (FD_ISSET(fd, &write_fds)) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100528 ret |= MBEDTLS_NET_POLL_WRITE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100529 }
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100531 return ret;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100532}
533
534/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 * Portable usleep helper
536 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100537void mbedtls_net_usleep(unsigned long usec)
Paul Bakker5121ce52009-01-03 21:22:43 +0000538{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200539#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 Sleep((usec + 999) / 1000);
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200541#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100543 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200544#if defined(__unix__) || defined(__unix) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 (defined(__APPLE__) && defined(__MACH__))
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100546 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200547#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100548 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200549#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 select(0, NULL, NULL, NULL, &tv);
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200551#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000552}
553
554/*
555 * Read at most 'len' characters
556 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100558{
Janos Follath865b3eb2019-12-16 11:46:15 +0000559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200560 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100562 ret = check_fd(fd, 0);
563 if (ret != 0) {
564 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 }
566
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 ret = (int) read(fd, buf, len);
568
569 if (ret < 0) {
570 if (net_would_block(ctx) != 0) {
571 return MBEDTLS_ERR_SSL_WANT_READ;
572 }
573
574#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
575 !defined(EFI32)
576 if (WSAGetLastError() == WSAECONNRESET) {
577 return MBEDTLS_ERR_NET_CONN_RESET;
578 }
579#else
580 if (errno == EPIPE || errno == ECONNRESET) {
581 return MBEDTLS_ERR_NET_CONN_RESET;
582 }
583
584 if (errno == EINTR) {
585 return MBEDTLS_ERR_SSL_WANT_READ;
586 }
587#endif
588
589 return MBEDTLS_ERR_NET_RECV_FAILED;
590 }
591
592 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000593}
594
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200595/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200596 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200597 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf,
599 size_t len, uint32_t timeout)
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200600{
Janos Follath865b3eb2019-12-16 11:46:15 +0000601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200602 struct timeval tv;
603 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200604 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 ret = check_fd(fd, 1);
607 if (ret != 0) {
608 return ret;
609 }
Gilles Peskineddf43742021-02-24 19:49:44 +0100610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 FD_ZERO(&read_fds);
Sergey20003ca2023-03-06 16:01:21 -0700612 FD_SET((SOCKET) fd, &read_fds);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200613
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200614 tv.tv_sec = timeout / 1000;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 tv.tv_usec = (timeout % 1000) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100617 ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200618
619 /* Zero fds ready means we timed out */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100620 if (ret == 0) {
621 return MBEDTLS_ERR_SSL_TIMEOUT;
622 }
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 if (ret < 0) {
625#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
626 !defined(EFI32)
627 if (WSAGetLastError() == WSAEINTR) {
628 return MBEDTLS_ERR_SSL_WANT_READ;
629 }
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200630#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100631 if (errno == EINTR) {
632 return MBEDTLS_ERR_SSL_WANT_READ;
633 }
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200634#endif
635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 return MBEDTLS_ERR_NET_RECV_FAILED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200637 }
638
639 /* This call will not block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100640 return mbedtls_net_recv(ctx, buf, len);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200641}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200642
Paul Bakker5121ce52009-01-03 21:22:43 +0000643/*
644 * Write at most 'len' characters
645 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100646int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000647{
Janos Follath865b3eb2019-12-16 11:46:15 +0000648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200649 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100651 ret = check_fd(fd, 0);
652 if (ret != 0) {
653 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 }
655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 ret = (int) write(fd, buf, len);
657
658 if (ret < 0) {
659 if (net_would_block(ctx) != 0) {
660 return MBEDTLS_ERR_SSL_WANT_WRITE;
661 }
662
663#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
664 !defined(EFI32)
665 if (WSAGetLastError() == WSAECONNRESET) {
666 return MBEDTLS_ERR_NET_CONN_RESET;
667 }
668#else
669 if (errno == EPIPE || errno == ECONNRESET) {
670 return MBEDTLS_ERR_NET_CONN_RESET;
671 }
672
673 if (errno == EINTR) {
674 return MBEDTLS_ERR_SSL_WANT_WRITE;
675 }
676#endif
677
678 return MBEDTLS_ERR_NET_SEND_FAILED;
679 }
680
681 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000682}
683
684/*
Robert Larsendf8e5112019-08-23 10:55:47 +0200685 * Close the connection
686 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100687void mbedtls_net_close(mbedtls_net_context *ctx)
Robert Larsendf8e5112019-08-23 10:55:47 +0200688{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689 if (ctx->fd == -1) {
Robert Larsendf8e5112019-08-23 10:55:47 +0200690 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 }
Robert Larsendf8e5112019-08-23 10:55:47 +0200692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 close(ctx->fd);
Robert Larsendf8e5112019-08-23 10:55:47 +0200694
695 ctx->fd = -1;
696}
697
698/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 * Gracefully close the connection
700 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100701void mbedtls_net_free(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000702{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100703 if (ctx->fd == -1) {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200704 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100705 }
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100707 shutdown(ctx->fd, 2);
708 close(ctx->fd);
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200709
710 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000711}
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#endif /* MBEDTLS_NET_C */