blob: 5d985ef00119bfd8f7c1a1bc3a8b60a3cb3dce4f [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Nicholas Wilson2682edf2017-12-05 12:08:15 +00008/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
9 * be set before config.h, which pulls in glibc's features.h indirectly.
10 * Harmless on other platforms. */
Flavio Ceolina79c30b2020-05-27 22:41:19 -070011#ifndef _POSIX_C_SOURCE
Nicholas Wilson2682edf2017-12-05 12:08:15 +000012#define _POSIX_C_SOURCE 200112L
Flavio Ceolina79c30b2020-05-27 22:41:19 -070013#endif
14#ifndef _XOPEN_SOURCE
nia0b01fd92020-06-11 12:29:15 +010015#define _XOPEN_SOURCE 600 /* sockaddr_storage */
Flavio Ceolina79c30b2020-05-27 22:41:19 -070016#endif
Nicholas Wilson2682edf2017-12-05 12:08:15 +000017
Gilles Peskinedb09ef62020-06-03 01:43:33 +020018#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010022#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040023 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000024 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010025#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
26#endif
27
SimonBd5800b72016-04-26 07:43:27 +010028#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010029
Andres AG788aa4a2016-09-14 14:32:09 +010030#include "mbedtls/net_sockets.h"
Janos Follath73c616b2019-12-18 15:07:04 +000031#include "mbedtls/error.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
36 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038#define IS_EINTR(ret) ((ret) == WSAEINTR)
Hanno Beckeref527962018-03-15 15:49:24 +000039
opatomic2b2acf12020-04-24 10:00:36 -040040#if !defined(_WIN32_WINNT)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010041/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010042#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020043#endif
44
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010045#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010046
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010047#include <winsock2.h>
48#include <windows.h>
opatomic2b2acf12020-04-24 10:00:36 -040049#if (_WIN32_WINNT < 0x0501)
50#include <wspiapi.h>
51#endif
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010052
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010053#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000054#if defined(_WIN32_WCE)
55#pragma comment( lib, "ws2.lib" )
56#else
57#pragma comment( lib, "ws2_32.lib" )
58#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010059#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061#define read(fd, buf, len) recv(fd, (char *) (buf), (int) (len), 0)
62#define write(fd, buf, len) send(fd, (char *) (buf), (int) (len), 0)
Paul Bakker5121ce52009-01-03 21:22:43 +000063#define close(fd) closesocket(fd)
64
65static int wsa_init_done = 0;
66
Paul Bakkerdb20c102014-06-17 14:34:44 +020067#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69#include <sys/types.h>
70#include <sys/socket.h>
71#include <netinet/in.h>
72#include <arpa/inet.h>
73#include <sys/time.h>
74#include <unistd.h>
75#include <signal.h>
76#include <fcntl.h>
77#include <netdb.h>
78#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080#define IS_EINTR(ret) ((ret) == EINTR)
Sergey20003ca2023-03-06 16:01:21 -070081#define SOCKET int
Hanno Beckeref527962018-03-15 15:49:24 +000082
Paul Bakkerdb20c102014-06-17 14:34:44 +020083#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020085/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010086 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020087#if defined(_MSC_VER)
88#define MSVC_INT_CAST (int)
89#else
90#define MSVC_INT_CAST
91#endif
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020094
Andrzej Kurek516e1b02022-03-02 10:55:08 -050095#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000096#include <time.h>
Andrzej Kurek516e1b02022-03-02 10:55:08 -050097#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020099#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100102 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104static int net_prepare(void)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100107 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 WSADATA wsaData;
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if (wsa_init_done == 0) {
111 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
112 return MBEDTLS_ERR_NET_SOCKET_FAILED;
113 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 wsa_init_done = 1;
116 }
117#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100118#if !defined(EFIX64) && !defined(EFI32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 signal(SIGPIPE, SIG_IGN);
Paul Bakker5121ce52009-01-03 21:22:43 +0000120#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100121#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 return 0;
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100123}
124
125/*
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200126 * Return 0 if the file descriptor is valid, an error otherwise.
127 * If for_select != 0, check whether the file descriptor is within the range
128 * allowed for fd_set used for the FD_xxx macros and the select() function.
129 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130static int check_fd(int fd, int for_select)
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200131{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 if (fd < 0) {
133 return MBEDTLS_ERR_NET_INVALID_CONTEXT;
134 }
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200135
Gilles Peskine51859aa2021-06-20 22:01:36 +0200136#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
137 !defined(EFI32)
138 (void) for_select;
139#else
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200140 /* A limitation of select() is that it only works with file descriptors
141 * that are strictly less than FD_SETSIZE. This is a limitation of the
142 * fd_set type. Error out early, because attempting to call FD_SET on a
143 * large file descriptor is a buffer overflow on typical platforms. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (for_select && fd >= FD_SETSIZE) {
145 return MBEDTLS_ERR_NET_POLL_FAILED;
146 }
Gilles Peskine51859aa2021-06-20 22:01:36 +0200147#endif
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 return 0;
Gilles Peskine0f6351f2021-06-20 23:08:19 +0200150}
151
152/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200153 * Initialize a context
154 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155void mbedtls_net_init(mbedtls_net_context *ctx)
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200156{
157 ctx->fd = -1;
158}
159
160/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100161 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100162 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
164 const char *port, int proto)
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100165{
Janos Follath865b3eb2019-12-16 11:46:15 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100167 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 if ((ret = net_prepare()) != 0) {
170 return ret;
171 }
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100172
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100173 /* Do name resolution with both IPv6 and IPv4 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 memset(&hints, 0, sizeof(hints));
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100175 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
177 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
180 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
181 }
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100182
183 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
186 ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
187 cur->ai_protocol);
188 if (ctx->fd < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100190 continue;
191 }
192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 if (connect(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) == 0) {
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100194 ret = 0;
195 break;
196 }
197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100200 }
201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 freeaddrinfo(addr_list);
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205}
206
207/*
208 * Create a listening socket on bind_ip:port
209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto)
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100212 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100213 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 if ((ret = net_prepare()) != 0) {
216 return ret;
217 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100218
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200219 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 memset(&hints, 0, sizeof(hints));
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100221 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
223 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 if (bind_ip == NULL) {
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100225 hints.ai_flags = AI_PASSIVE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100227
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 if (getaddrinfo(bind_ip, port, &hints, &addr_list) != 0) {
229 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
230 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100231
232 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
235 ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
236 cur->ai_protocol);
237 if (ctx->fd < 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100239 continue;
240 }
241
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100242 n = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
244 (const char *) &n, sizeof(n)) != 0) {
245 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200247 continue;
248 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
251 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100253 continue;
254 }
255
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100256 /* Listen only makes sense for TCP */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 if (proto == MBEDTLS_NET_PROTO_TCP) {
258 if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
259 close(ctx->fd);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100261 continue;
262 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100263 }
264
Brian J Murray2adecba2016-11-06 04:45:15 -0800265 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100266 ret = 0;
267 break;
268 }
269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 freeaddrinfo(addr_list);
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274}
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100277 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100278/*
279 * Check if the requested operation would be blocking on a non-blocking socket
280 * and thus 'failed' with a negative return value.
281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282static int net_would_block(const mbedtls_net_context *ctx)
Paul Bakker80025412014-01-23 20:59:49 +0100283{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200284 ((void) ctx);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 return WSAGetLastError() == WSAEWOULDBLOCK;
Paul Bakker80025412014-01-23 20:59:49 +0100286}
Paul Bakker5121ce52009-01-03 21:22:43 +0000287#else
Paul Bakker80025412014-01-23 20:59:49 +0100288/*
289 * Check if the requested operation would be blocking on a non-blocking socket
290 * and thus 'failed' with a negative return value.
291 *
292 * Note: on a blocking socket this function always returns 0!
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{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100296 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100297
Paul Bakker80025412014-01-23 20:59:49 +0100298 /*
Jaeden Ameroa152e422019-05-29 09:38:29 +0100299 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100300 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 if ((fcntl(ctx->fd, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100302 errno = err;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 return 0;
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100304 }
Paul Bakker80025412014-01-23 20:59:49 +0100305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 switch (errno = err) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000307#if defined EAGAIN
308 case EAGAIN:
309#endif
310#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
311 case EWOULDBLOCK:
312#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 return 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000316}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200317#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319/*
320 * Accept a connection from a remote client
321 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
323 mbedtls_net_context *client_ctx,
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000324 void *client_ip, size_t buf_size, size_t *cip_len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000325{
Janos Follath865b3eb2019-12-16 11:46:15 +0000326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100327 int type;
328
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100329 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Paul Bakker394c56f2011-12-20 12:19:03 +0000331#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
nia0b01fd92020-06-11 12:29:15 +0100332 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
okhowang(王沛文)76158ce2020-09-03 15:36:36 +0800333 defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 socklen_t n = (socklen_t) sizeof(client_addr);
335 socklen_t type_len = (socklen_t) sizeof(type);
Paul Bakker5121ce52009-01-03 21:22:43 +0000336#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 int n = (int) sizeof(client_addr);
338 int type_len = (int) sizeof(type);
Paul Bakker5121ce52009-01-03 21:22:43 +0000339#endif
340
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100341 /* Is this a TCP or UDP socket? */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE,
343 (void *) &type, &type_len) != 0 ||
344 (type != SOCK_STREAM && type != SOCK_DGRAM)) {
345 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100346 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 if (type == SOCK_STREAM) {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100349 /* TCP: actual accept() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 ret = client_ctx->fd = (int) accept(bind_ctx->fd,
351 (struct sockaddr *) &client_addr, &n);
352 } else {
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100353 /* UDP: wait for a message, but keep it in the queue */
354 char buf[1] = { 0 };
355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 ret = (int) recvfrom(bind_ctx->fd, buf, sizeof(buf), MSG_PEEK,
357 (struct sockaddr *) &client_addr, &n);
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200358
359#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (ret == SOCKET_ERROR &&
361 WSAGetLastError() == WSAEMSGSIZE) {
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200362 /* We know buf is too small, thanks, just peeking here */
363 ret = 0;
364 }
365#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100366 }
367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 if (ret < 0) {
369 if (net_would_block(bind_ctx) != 0) {
370 return MBEDTLS_ERR_SSL_WANT_READ;
371 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 }
375
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200376 /* UDP: hijack the listening socket to communicate with the client,
377 * then bind a new socket to accept new connections */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 if (type != SOCK_STREAM) {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200379 struct sockaddr_storage local_addr;
380 int one = 1;
381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (connect(bind_ctx->fd, (struct sockaddr *) &client_addr, n) != 0) {
383 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
384 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100385
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200386 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200387 bind_ctx->fd = -1; /* In case we exit early */
388
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 n = sizeof(struct sockaddr_storage);
390 if (getsockname(client_ctx->fd,
391 (struct sockaddr *) &local_addr, &n) != 0 ||
392 (bind_ctx->fd = (int) socket(local_addr.ss_family,
393 SOCK_DGRAM, IPPROTO_UDP)) < 0 ||
394 setsockopt(bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
395 (const char *) &one, sizeof(one)) != 0) {
396 return MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200397 }
398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100399 if (bind(bind_ctx->fd, (struct sockaddr *) &local_addr, n) != 0) {
400 return MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200401 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100402 }
403
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 if (client_ip != NULL) {
405 if (client_addr.ss_family == AF_INET) {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100406 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000407 *cip_len = sizeof(addr4->sin_addr.s_addr);
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200408
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000409 if (buf_size < *cip_len) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
411 }
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200412
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000413 memcpy(client_ip, &addr4->sin_addr.s_addr, *cip_len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414 } else {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100415 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000416 *cip_len = sizeof(addr6->sin6_addr.s6_addr);
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200417
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000418 if (buf_size < *cip_len) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
420 }
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200421
Tom Cosgrovee1f6d3b2023-12-08 21:53:18 +0000422 memcpy(client_ip, &addr6->sin6_addr.s6_addr, *cip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100423 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100424 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000427}
428
429/*
430 * Set the socket blocking or non-blocking
431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432int mbedtls_net_set_block(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000433{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100435 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000436 u_long n = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 return ioctlsocket(ctx->fd, FIONBIO, &n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000438#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) & ~O_NONBLOCK);
Paul Bakker5121ce52009-01-03 21:22:43 +0000440#endif
441}
442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000444{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100446 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000447 u_long n = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 return ioctlsocket(ctx->fd, FIONBIO, &n);
Paul Bakker5121ce52009-01-03 21:22:43 +0000449#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) | O_NONBLOCK);
Paul Bakker5121ce52009-01-03 21:22:43 +0000451#endif
452}
453
454/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100455 * Check if data is available on the socket
456 */
457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100459{
Janos Follath865b3eb2019-12-16 11:46:15 +0000460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100461 struct timeval tv;
462
463 fd_set read_fds;
464 fd_set write_fds;
465
466 int fd = ctx->fd;
467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 ret = check_fd(fd, 1);
469 if (ret != 0) {
470 return ret;
471 }
Gilles Peskineddf43742021-02-24 19:49:44 +0100472
Gilles Peskineec4733b2018-04-05 14:55:47 +0200473#if defined(__has_feature)
474#if __has_feature(memory_sanitizer)
475 /* Ensure that memory sanitizers consider read_fds and write_fds as
476 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
477 * is implemented in assembly. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 memset(&read_fds, 0, sizeof(read_fds));
479 memset(&write_fds, 0, sizeof(write_fds));
Gilles Peskineec4733b2018-04-05 14:55:47 +0200480#endif
481#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 FD_ZERO(&read_fds);
484 if (rw & MBEDTLS_NET_POLL_READ) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100485 rw &= ~MBEDTLS_NET_POLL_READ;
Sergey20003ca2023-03-06 16:01:21 -0700486 FD_SET((SOCKET) fd, &read_fds);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100487 }
488
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 FD_ZERO(&write_fds);
490 if (rw & MBEDTLS_NET_POLL_WRITE) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100491 rw &= ~MBEDTLS_NET_POLL_WRITE;
Sergey20003ca2023-03-06 16:01:21 -0700492 FD_SET((SOCKET) fd, &write_fds);
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100493 }
494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (rw != 0) {
496 return MBEDTLS_ERR_NET_BAD_INPUT_DATA;
497 }
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100498
499 tv.tv_sec = timeout / 1000;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 tv.tv_usec = (timeout % 1000) * 1000;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100501
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 do {
503 ret = select(fd + 1, &read_fds, &write_fds, NULL,
504 timeout == (uint32_t) -1 ? NULL : &tv);
505 } while (IS_EINTR(ret));
506
507 if (ret < 0) {
508 return MBEDTLS_ERR_NET_POLL_FAILED;
Hanno Becker80e06d72018-03-15 14:41:55 +0000509 }
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100510
511 ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100512 if (FD_ISSET(fd, &read_fds)) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100513 ret |= MBEDTLS_NET_POLL_READ;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100514 }
515 if (FD_ISSET(fd, &write_fds)) {
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100516 ret |= MBEDTLS_NET_POLL_WRITE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 }
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 return ret;
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100520}
521
522/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 * Portable usleep helper
524 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100525void mbedtls_net_usleep(unsigned long usec)
Paul Bakker5121ce52009-01-03 21:22:43 +0000526{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200527#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 Sleep((usec + 999) / 1000);
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200529#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100531 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200532#if defined(__unix__) || defined(__unix) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100533 (defined(__APPLE__) && defined(__MACH__))
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100534 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200535#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100536 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200537#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 select(0, NULL, NULL, NULL, &tv);
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000540}
541
542/*
543 * Read at most 'len' characters
544 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100546{
Janos Follath865b3eb2019-12-16 11:46:15 +0000547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200548 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 ret = check_fd(fd, 0);
551 if (ret != 0) {
552 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 }
554
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 ret = (int) read(fd, buf, len);
556
557 if (ret < 0) {
558 if (net_would_block(ctx) != 0) {
559 return MBEDTLS_ERR_SSL_WANT_READ;
560 }
561
562#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
563 !defined(EFI32)
564 if (WSAGetLastError() == WSAECONNRESET) {
565 return MBEDTLS_ERR_NET_CONN_RESET;
566 }
567#else
568 if (errno == EPIPE || errno == ECONNRESET) {
569 return MBEDTLS_ERR_NET_CONN_RESET;
570 }
571
572 if (errno == EINTR) {
573 return MBEDTLS_ERR_SSL_WANT_READ;
574 }
575#endif
576
577 return MBEDTLS_ERR_NET_RECV_FAILED;
578 }
579
580 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000581}
582
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200583/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200584 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200585 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf,
587 size_t len, uint32_t timeout)
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200588{
Janos Follath865b3eb2019-12-16 11:46:15 +0000589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200590 struct timeval tv;
591 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200592 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100594 ret = check_fd(fd, 1);
595 if (ret != 0) {
596 return ret;
597 }
Gilles Peskineddf43742021-02-24 19:49:44 +0100598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 FD_ZERO(&read_fds);
Sergey20003ca2023-03-06 16:01:21 -0700600 FD_SET((SOCKET) fd, &read_fds);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200601
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200602 tv.tv_sec = timeout / 1000;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 tv.tv_usec = (timeout % 1000) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200606
607 /* Zero fds ready means we timed out */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 if (ret == 0) {
609 return MBEDTLS_ERR_SSL_TIMEOUT;
610 }
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612 if (ret < 0) {
613#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
614 !defined(EFI32)
615 if (WSAGetLastError() == WSAEINTR) {
616 return MBEDTLS_ERR_SSL_WANT_READ;
617 }
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200618#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619 if (errno == EINTR) {
620 return MBEDTLS_ERR_SSL_WANT_READ;
621 }
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200622#endif
623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624 return MBEDTLS_ERR_NET_RECV_FAILED;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200625 }
626
627 /* This call will not block */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100628 return mbedtls_net_recv(ctx, buf, len);
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200629}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200630
Paul Bakker5121ce52009-01-03 21:22:43 +0000631/*
632 * Write at most 'len' characters
633 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100634int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000635{
Janos Follath865b3eb2019-12-16 11:46:15 +0000636 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200637 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100639 ret = check_fd(fd, 0);
640 if (ret != 0) {
641 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 ret = (int) write(fd, buf, len);
645
646 if (ret < 0) {
647 if (net_would_block(ctx) != 0) {
648 return MBEDTLS_ERR_SSL_WANT_WRITE;
649 }
650
651#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
652 !defined(EFI32)
653 if (WSAGetLastError() == WSAECONNRESET) {
654 return MBEDTLS_ERR_NET_CONN_RESET;
655 }
656#else
657 if (errno == EPIPE || errno == ECONNRESET) {
658 return MBEDTLS_ERR_NET_CONN_RESET;
659 }
660
661 if (errno == EINTR) {
662 return MBEDTLS_ERR_SSL_WANT_WRITE;
663 }
664#endif
665
666 return MBEDTLS_ERR_NET_SEND_FAILED;
667 }
668
669 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670}
671
672/*
Robert Larsendf8e5112019-08-23 10:55:47 +0200673 * Close the connection
674 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100675void mbedtls_net_close(mbedtls_net_context *ctx)
Robert Larsendf8e5112019-08-23 10:55:47 +0200676{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100677 if (ctx->fd == -1) {
Robert Larsendf8e5112019-08-23 10:55:47 +0200678 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100679 }
Robert Larsendf8e5112019-08-23 10:55:47 +0200680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100681 close(ctx->fd);
Robert Larsendf8e5112019-08-23 10:55:47 +0200682
683 ctx->fd = -1;
684}
685
686/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 * Gracefully close the connection
688 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100689void mbedtls_net_free(mbedtls_net_context *ctx)
Paul Bakker5121ce52009-01-03 21:22:43 +0000690{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100691 if (ctx->fd == -1) {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200692 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100693 }
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200694
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100695 shutdown(ctx->fd, 2);
696 close(ctx->fd);
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200697
698 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000699}
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_NET_C */