blob: 70b3f125cef1b9c203fc4c0d691dcbba585df616 [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
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020021 * be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022 * Harmless on other platforms. */
Flavio Ceolinb0257032020-05-27 22:41:19 -070023#ifndef _POSIX_C_SOURCE
Nicholas Wilson2682edf2017-12-05 12:08:15 +000024#define _POSIX_C_SOURCE 200112L
Flavio Ceolinb0257032020-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 Ceolinb0257032020-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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020034#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__)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037# error \
38 "This module only works on Unix and Windows, see MBEDTLS_NET_C in mbedtls_config.h"
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010039#endif
40
SimonBd5800b72016-04-26 07:43:27 +010041#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010043#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# 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"
Janos Follath73c616b2019-12-18 15:07:04 +000048#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Paul Bakkerfa6a6202013-10-28 18:48:30 +010052#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
53 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020055# define IS_EINTR(ret) ((ret) == WSAEINTR)
Hanno Beckeref527962018-03-15 15:49:24 +000056
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057# if !defined(_WIN32_WINNT)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010058/* Enables getaddrinfo() & Co */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059# define _WIN32_WINNT 0x0501
60# endif
Fabio Alessandrellidf608562018-04-03 19:40:11 +020061
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062# include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010063
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020064# include <winsock2.h>
65# include <windows.h>
66# if (_WIN32_WINNT < 0x0501)
67# include <wspiapi.h>
68# endif
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010069
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070# if defined(_MSC_VER)
71# if defined(_WIN32_WCE)
72# pragma comment(lib, "ws2.lib")
73# else
74# pragma comment(lib, "ws2_32.lib")
75# endif
76# endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078# define read(fd, buf, len) recv(fd, (char *)(buf), (int)(len), 0)
79# define write(fd, buf, len) send(fd, (char *)(buf), (int)(len), 0)
80# define close(fd) closesocket(fd)
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82static int wsa_init_done = 0;
83
Paul Bakkerdb20c102014-06-17 14:34:44 +020084#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086# include <sys/types.h>
87# include <sys/socket.h>
88# include <netinet/in.h>
89# include <arpa/inet.h>
90# include <sys/time.h>
91# include <unistd.h>
92# include <signal.h>
93# include <fcntl.h>
94# include <netdb.h>
95# include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000096
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097# define IS_EINTR(ret) ((ret) == EINTR)
Hanno Beckeref527962018-03-15 15:49:24 +000098
Paul Bakkerdb20c102014-06-17 14:34:44 +020099#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200101/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100102 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200103#if defined(_MSC_VER)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104# define MSVC_INT_CAST (int)
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200105#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106# define MSVC_INT_CAST
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200107#endif
108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111#include <time.h>
112
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200113#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000114
Paul Bakker5121ce52009-01-03 21:22:43 +0000115/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100116 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118static int net_prepare(void)
Paul Bakker5121ce52009-01-03 21:22:43 +0000119{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100121 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 WSADATA wsaData;
123
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 if (wsa_init_done == 0) {
125 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
126 return MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128 wsa_init_done = 1;
129 }
130#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200131# if !defined(EFIX64) && !defined(EFI32)
132 signal(SIGPIPE, SIG_IGN);
133# endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000134#endif
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135 return 0;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100136}
137
138/*
Gilles Peskine05360002021-06-20 23:08:19 +0200139 * Return 0 if the file descriptor is valid, an error otherwise.
140 * If for_select != 0, check whether the file descriptor is within the range
141 * allowed for fd_set used for the FD_xxx macros and the select() function.
142 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200143static int check_fd(int fd, int for_select)
Gilles Peskine05360002021-06-20 23:08:19 +0200144{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145 if (fd < 0)
146 return MBEDTLS_ERR_NET_INVALID_CONTEXT;
Gilles Peskine05360002021-06-20 23:08:19 +0200147
Gilles Peskinea5dd7bd2021-06-20 22:01:36 +0200148#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
149 !defined(EFI32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200150 (void)for_select;
Gilles Peskinea5dd7bd2021-06-20 22:01:36 +0200151#else
Gilles Peskine05360002021-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. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 if (for_select && fd >= FD_SETSIZE)
157 return MBEDTLS_ERR_NET_POLL_FAILED;
Gilles Peskinea5dd7bd2021-06-20 22:01:36 +0200158#endif
Gilles Peskine05360002021-06-20 23:08:19 +0200159
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160 return 0;
Gilles Peskine05360002021-06-20 23:08:19 +0200161}
162
163/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200164 * Initialize a context
165 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200166void mbedtls_net_init(mbedtls_net_context *ctx)
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200167{
168 ctx->fd = -1;
169}
170
171/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100172 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100173 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200174int mbedtls_net_connect(mbedtls_net_context *ctx,
175 const char *host,
176 const char *port,
177 int proto)
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100178{
Janos Follath865b3eb2019-12-16 11:46:15 +0000179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100180 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100181
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182 if ((ret = net_prepare()) != 0)
183 return ret;
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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 memset(&hints, 0, sizeof(hints));
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187 hints.ai_family = AF_UNSPEC;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200188 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM :
189 SOCK_STREAM;
190 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP :
191 IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 if (getaddrinfo(host, port, &hints, &addr_list) != 0)
194 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100195
196 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200198 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
199 ctx->fd =
200 (int)socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
201 if (ctx->fd < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100203 continue;
204 }
205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 if (connect(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) ==
207 0) {
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100208 ret = 0;
209 break;
210 }
211
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100214 }
215
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216 freeaddrinfo(addr_list);
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100217
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200218 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219}
220
221/*
222 * Create a listening socket on bind_ip:port
223 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224int mbedtls_net_bind(mbedtls_net_context *ctx,
225 const char *bind_ip,
226 const char *port,
227 int proto)
Paul Bakker5121ce52009-01-03 21:22:43 +0000228{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100229 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100230 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100231
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200232 if ((ret = net_prepare()) != 0)
233 return ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100234
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200235 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 memset(&hints, 0, sizeof(hints));
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100237 hints.ai_family = AF_UNSPEC;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200238 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM :
239 SOCK_STREAM;
240 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP :
241 IPPROTO_TCP;
242 if (bind_ip == NULL)
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100243 hints.ai_flags = AI_PASSIVE;
244
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200245 if (getaddrinfo(bind_ip, port, &hints, &addr_list) != 0)
246 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100247
248 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200250 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
251 ctx->fd =
252 (int)socket(cur->ai_family, cur->ai_socktype, cur->ai_protocol);
253 if (ctx->fd < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100255 continue;
256 }
257
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100258 n = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259 if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&n,
260 sizeof(n)) != 0) {
261 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200263 continue;
264 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100265
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200266 if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
267 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269 continue;
270 }
271
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100272 /* Listen only makes sense for TCP */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273 if (proto == MBEDTLS_NET_PROTO_TCP) {
274 if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
275 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100277 continue;
278 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100279 }
280
Brian J Murray2adecba2016-11-06 04:45:15 -0800281 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100282 ret = 0;
283 break;
284 }
285
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286 freeaddrinfo(addr_list);
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100287
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200288 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000289}
290
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200291#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100292 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100293/*
294 * Check if the requested operation would be blocking on a non-blocking socket
295 * and thus 'failed' with a negative return value.
296 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297static int net_would_block(const mbedtls_net_context *ctx)
Paul Bakker80025412014-01-23 20:59:49 +0100298{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200299 ((void)ctx);
300 return WSAGetLastError() == WSAEWOULDBLOCK;
Paul Bakker80025412014-01-23 20:59:49 +0100301}
Paul Bakker5121ce52009-01-03 21:22:43 +0000302#else
Paul Bakker80025412014-01-23 20:59:49 +0100303/*
304 * Check if the requested operation would be blocking on a non-blocking socket
305 * and thus 'failed' with a negative return value.
306 *
307 * Note: on a blocking socket this function always returns 0!
308 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200309static int net_would_block(const mbedtls_net_context *ctx)
Paul Bakker80025412014-01-23 20:59:49 +0100310{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100311 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100312
Paul Bakker80025412014-01-23 20:59:49 +0100313 /*
Jaeden Ameroa152e422019-05-29 09:38:29 +0100314 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100315 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200316 if ((fcntl(ctx->fd, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100317 errno = err;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200318 return 0;
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100319 }
Paul Bakker80025412014-01-23 20:59:49 +0100320
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200321 switch (errno = err) {
322# if defined EAGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 case EAGAIN:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200324# endif
325# if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 case EWOULDBLOCK:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200327# endif
328 return 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200330 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000331}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200332#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334/*
335 * Accept a connection from a remote client
336 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
338 mbedtls_net_context *client_ctx,
339 void *client_ip,
340 size_t buf_size,
341 size_t *ip_len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000342{
Janos Follath865b3eb2019-12-16 11:46:15 +0000343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100344 int type;
345
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100346 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
nia0b01fd92020-06-11 12:29:15 +0100349 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200350 defined(socklen_t) || \
351 (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
352 socklen_t n = (socklen_t)sizeof(client_addr);
353 socklen_t type_len = (socklen_t)sizeof(type);
Paul Bakker5121ce52009-01-03 21:22:43 +0000354#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200355 int n = (int)sizeof(client_addr);
356 int type_len = (int)sizeof(type);
Paul Bakker5121ce52009-01-03 21:22:43 +0000357#endif
358
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100359 /* Is this a TCP or UDP socket? */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE, (void *)&type,
361 &type_len) != 0 ||
362 (type != SOCK_STREAM && type != SOCK_DGRAM)) {
363 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100364 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366 if (type == SOCK_STREAM) {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100367 /* TCP: actual accept() */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200368 ret = client_ctx->fd =
369 (int)accept(bind_ctx->fd, (struct sockaddr *)&client_addr, &n);
370 } else {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100371 /* UDP: wait for a message, but keep it in the queue */
372 char buf[1] = { 0 };
373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374 ret = (int)recvfrom(bind_ctx->fd, buf, sizeof(buf), MSG_PEEK,
375 (struct sockaddr *)&client_addr, &n);
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200376
377#if defined(_WIN32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378 if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEMSGSIZE) {
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200379 /* We know buf is too small, thanks, just peeking here */
380 ret = 0;
381 }
382#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100383 }
384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200385 if (ret < 0) {
386 if (net_would_block(bind_ctx) != 0)
387 return MBEDTLS_ERR_SSL_WANT_READ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200389 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 }
391
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200392 /* UDP: hijack the listening socket to communicate with the client,
393 * then bind a new socket to accept new connections */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 if (type != SOCK_STREAM) {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200395 struct sockaddr_storage local_addr;
396 int one = 1;
397
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200398 if (connect(bind_ctx->fd, (struct sockaddr *)&client_addr, n) != 0)
399 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100400
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200401 client_ctx->fd = bind_ctx->fd;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200402 bind_ctx->fd = -1; /* In case we exit early */
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200403
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200404 n = sizeof(struct sockaddr_storage);
405 if (getsockname(client_ctx->fd, (struct sockaddr *)&local_addr, &n) !=
406 0 ||
407 (bind_ctx->fd = (int)socket(local_addr.ss_family, SOCK_DGRAM,
408 IPPROTO_UDP)) < 0 ||
409 setsockopt(bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
410 (const char *)&one, sizeof(one)) != 0) {
411 return MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200412 }
413
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200414 if (bind(bind_ctx->fd, (struct sockaddr *)&local_addr, n) != 0) {
415 return MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200416 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100417 }
418
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200419 if (client_ip != NULL) {
420 if (client_addr.ss_family == AF_INET) {
421 struct sockaddr_in *addr4 = (struct sockaddr_in *)&client_addr;
422 *ip_len = sizeof(addr4->sin_addr.s_addr);
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200423
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200424 if (buf_size < *ip_len)
425 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200426
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200427 memcpy(client_ip, &addr4->sin_addr.s_addr, *ip_len);
428 } else {
429 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&client_addr;
430 *ip_len = sizeof(addr6->sin6_addr.s6_addr);
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200431
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200432 if (buf_size < *ip_len)
433 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200434
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200435 memcpy(client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100436 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100437 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200439 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000440}
441
442/*
443 * Set the socket blocking or non-blocking
444 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200445int mbedtls_net_set_block(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000446{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100448 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000449 u_long n = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200450 return ioctlsocket(ctx->fd, FIONBIO, &n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000451#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200452 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) & ~O_NONBLOCK);
Paul Bakker5121ce52009-01-03 21:22:43 +0000453#endif
454}
455
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200456int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000457{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200458#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100459 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000460 u_long n = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200461 return ioctlsocket(ctx->fd, FIONBIO, &n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000462#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200463 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) | O_NONBLOCK);
Paul Bakker5121ce52009-01-03 21:22:43 +0000464#endif
465}
466
467/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100468 * Check if data is available on the socket
469 */
470
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200471int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100472{
Janos Follath865b3eb2019-12-16 11:46:15 +0000473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100474 struct timeval tv;
475
476 fd_set read_fds;
477 fd_set write_fds;
478
479 int fd = ctx->fd;
480
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200481 ret = check_fd(fd, 1);
482 if (ret != 0)
483 return ret;
Gilles Peskineddf43742021-02-24 19:49:44 +0100484
Gilles Peskineec4733b2018-04-05 14:55:47 +0200485#if defined(__has_feature)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200486# if __has_feature(memory_sanitizer)
Gilles Peskineec4733b2018-04-05 14:55:47 +0200487 /* 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. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490 memset(&read_fds, 0, sizeof(read_fds));
491 memset(&write_fds, 0, sizeof(write_fds));
492# endif
Gilles Peskineec4733b2018-04-05 14:55:47 +0200493#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100494
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200495 FD_ZERO(&read_fds);
496 if (rw & MBEDTLS_NET_POLL_READ) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100497 rw &= ~MBEDTLS_NET_POLL_READ;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200498 FD_SET(fd, &read_fds);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100499 }
500
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200501 FD_ZERO(&write_fds);
502 if (rw & MBEDTLS_NET_POLL_WRITE) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100503 rw &= ~MBEDTLS_NET_POLL_WRITE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200504 FD_SET(fd, &write_fds);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100505 }
506
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200507 if (rw != 0)
508 return MBEDTLS_ERR_NET_BAD_INPUT_DATA;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100509
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200510 tv.tv_sec = timeout / 1000;
511 tv.tv_usec = (timeout % 1000) * 1000;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100512
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200513 do {
514 ret = select(fd + 1, &read_fds, &write_fds, NULL,
515 timeout == (uint32_t)-1 ? NULL : &tv);
516 } while (IS_EINTR(ret));
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100517
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200518 if (ret < 0)
519 return MBEDTLS_ERR_NET_POLL_FAILED;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100520
521 ret = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200522 if (FD_ISSET(fd, &read_fds))
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100523 ret |= MBEDTLS_NET_POLL_READ;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200524 if (FD_ISSET(fd, &write_fds))
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100525 ret |= MBEDTLS_NET_POLL_WRITE;
526
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200527 return ret;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100528}
529
530/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * Portable usleep helper
532 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200533void mbedtls_net_usleep(unsigned long usec)
Paul Bakker5121ce52009-01-03 21:22:43 +0000534{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200535#if defined(_WIN32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200536 Sleep((usec + 999) / 1000);
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200537#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 struct timeval tv;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200539 tv.tv_sec = usec / 1000000;
540# if defined(__unix__) || defined(__unix) || \
541 (defined(__APPLE__) && defined(__MACH__))
542 tv.tv_usec = (suseconds_t)usec % 1000000;
543# else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100544 tv.tv_usec = usec % 1000000;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200545# endif
546 select(0, NULL, NULL, NULL, &tv);
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200547#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000548}
549
550/*
551 * Read at most 'len' characters
552 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200553int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100554{
Janos Follath865b3eb2019-12-16 11:46:15 +0000555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200556 int fd = ((mbedtls_net_context *)ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200557
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200558 ret = check_fd(fd, 0);
559 if (ret != 0)
560 return ret;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200561
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200562 ret = (int)read(fd, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200564 if (ret < 0) {
565 if (net_would_block(ctx) != 0)
566 return MBEDTLS_ERR_SSL_WANT_READ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200568#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100569 !defined(EFI32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200570 if (WSAGetLastError() == WSAECONNRESET)
571 return MBEDTLS_ERR_NET_CONN_RESET;
Paul Bakker5121ce52009-01-03 21:22:43 +0000572#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200573 if (errno == EPIPE || errno == ECONNRESET)
574 return MBEDTLS_ERR_NET_CONN_RESET;
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200576 if (errno == EINTR)
577 return MBEDTLS_ERR_SSL_WANT_READ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000578#endif
579
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200580 return MBEDTLS_ERR_NET_RECV_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 }
582
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200583 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000584}
585
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200586/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200587 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200588 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200589int mbedtls_net_recv_timeout(void *ctx,
590 unsigned char *buf,
591 size_t len,
592 uint32_t timeout)
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200593{
Janos Follath865b3eb2019-12-16 11:46:15 +0000594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200595 struct timeval tv;
596 fd_set read_fds;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200597 int fd = ((mbedtls_net_context *)ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200598
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200599 ret = check_fd(fd, 1);
600 if (ret != 0)
601 return ret;
Gilles Peskineddf43742021-02-24 19:49:44 +0100602
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200603 FD_ZERO(&read_fds);
604 FD_SET(fd, &read_fds);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200605
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200606 tv.tv_sec = timeout / 1000;
607 tv.tv_usec = (timeout % 1000) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200608
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609 ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200610
611 /* Zero fds ready means we timed out */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200612 if (ret == 0)
613 return MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200614
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200615 if (ret < 0) {
616#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200617 !defined(EFI32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200618 if (WSAGetLastError() == WSAEINTR)
619 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200620#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621 if (errno == EINTR)
622 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200623#endif
624
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200625 return MBEDTLS_ERR_NET_RECV_FAILED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200626 }
627
628 /* This call will not block */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200629 return mbedtls_net_recv(ctx, buf, len);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200630}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200631
Paul Bakker5121ce52009-01-03 21:22:43 +0000632/*
633 * Write at most 'len' characters
634 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200635int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000636{
Janos Follath865b3eb2019-12-16 11:46:15 +0000637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200638 int fd = ((mbedtls_net_context *)ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200639
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200640 ret = check_fd(fd, 0);
641 if (ret != 0)
642 return ret;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200643
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200644 ret = (int)write(fd, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200646 if (ret < 0) {
647 if (net_would_block(ctx) != 0)
648 return MBEDTLS_ERR_SSL_WANT_WRITE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200650#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100651 !defined(EFI32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200652 if (WSAGetLastError() == WSAECONNRESET)
653 return MBEDTLS_ERR_NET_CONN_RESET;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200655 if (errno == EPIPE || errno == ECONNRESET)
656 return MBEDTLS_ERR_NET_CONN_RESET;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200658 if (errno == EINTR)
659 return MBEDTLS_ERR_SSL_WANT_WRITE;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660#endif
661
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200662 return MBEDTLS_ERR_NET_SEND_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
664
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200665 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666}
667
668/*
Robert Larsendf8e5112019-08-23 10:55:47 +0200669 * Close the connection
670 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200671void mbedtls_net_close(mbedtls_net_context *ctx)
Robert Larsendf8e5112019-08-23 10:55:47 +0200672{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200673 if (ctx->fd == -1)
Robert Larsendf8e5112019-08-23 10:55:47 +0200674 return;
675
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200676 close(ctx->fd);
Robert Larsendf8e5112019-08-23 10:55:47 +0200677
678 ctx->fd = -1;
679}
680
681/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 * Gracefully close the connection
683 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200684void mbedtls_net_free(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000685{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200686 if (ctx->fd == -1)
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200687 return;
688
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200689 shutdown(ctx->fd, 2);
690 close(ctx->fd);
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200691
692 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000693}
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#endif /* MBEDTLS_NET_C */