blob: edec5876ad8a3431710d6d894d9704a7cad0ea7c [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * TCP/IP or UDP/IP networking functions
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
Tom Van Eyckc1633172024-04-09 18:44:13 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +02006 */
7
Jens Wiklander3d3b0592019-03-20 15:30:29 +01008/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
Jens Wiklander32b31802023-10-06 16:59:46 +02009 * be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
Jens Wiklander3d3b0592019-03-20 15:30:29 +010010 * Harmless on other platforms. */
Jerome Forissier79013242021-07-28 10:24:04 +020011#ifndef _POSIX_C_SOURCE
Jens Wiklander3d3b0592019-03-20 15:30:29 +010012#define _POSIX_C_SOURCE 200112L
Jens Wiklander817466c2018-05-22 13:49:31 +020013#endif
Jerome Forissier79013242021-07-28 10:24:04 +020014#ifndef _XOPEN_SOURCE
15#define _XOPEN_SOURCE 600 /* sockaddr_storage */
16#endif
17
18#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020019
20#if defined(MBEDTLS_NET_C)
21
22#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +010023 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Jerome Forissier79013242021-07-28 10:24:04 +020024 !defined(__HAIKU__) && !defined(__midipix__)
Jens Wiklander32b31802023-10-06 16:59:46 +020025#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in mbedtls_config.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020026#endif
27
Jens Wiklander817466c2018-05-22 13:49:31 +020028#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020029
30#include "mbedtls/net_sockets.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020031#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020032
33#include <string.h>
34
35#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
36 !defined(EFI32)
37
Jens Wiklander32b31802023-10-06 16:59:46 +020038#define IS_EINTR(ret) ((ret) == WSAEINTR)
Jens Wiklander3d3b0592019-03-20 15:30:29 +010039
Jens Wiklander817466c2018-05-22 13:49:31 +020040#include <ws2tcpip.h>
41
42#include <winsock2.h>
43#include <windows.h>
Jerome Forissier79013242021-07-28 10:24:04 +020044#if (_WIN32_WINNT < 0x0501)
45#include <wspiapi.h>
46#endif
Jens Wiklander817466c2018-05-22 13:49:31 +020047
48#if defined(_MSC_VER)
49#if defined(_WIN32_WCE)
50#pragma comment( lib, "ws2.lib" )
51#else
52#pragma comment( lib, "ws2_32.lib" )
53#endif
54#endif /* _MSC_VER */
55
Jens Wiklander32b31802023-10-06 16:59:46 +020056#define read(fd, buf, len) recv(fd, (char *) (buf), (int) (len), 0)
57#define write(fd, buf, len) send(fd, (char *) (buf), (int) (len), 0)
Jens Wiklander817466c2018-05-22 13:49:31 +020058#define close(fd) closesocket(fd)
59
60static int wsa_init_done = 0;
61
62#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
63
64#include <sys/types.h>
65#include <sys/socket.h>
66#include <netinet/in.h>
67#include <arpa/inet.h>
68#include <sys/time.h>
69#include <unistd.h>
70#include <signal.h>
71#include <fcntl.h>
72#include <netdb.h>
73#include <errno.h>
74
Jens Wiklander32b31802023-10-06 16:59:46 +020075#define IS_EINTR(ret) ((ret) == EINTR)
Tom Van Eyckc1633172024-04-09 18:44:13 +020076#define SOCKET int
Jens Wiklander3d3b0592019-03-20 15:30:29 +010077
Jens Wiklander817466c2018-05-22 13:49:31 +020078#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
79
80/* Some MS functions want int and MSVC warns if we pass size_t,
Jens Wiklander3d3b0592019-03-20 15:30:29 +010081 * but the standard functions use socklen_t, so cast only for MSVC */
Jens Wiklander817466c2018-05-22 13:49:31 +020082#if defined(_MSC_VER)
83#define MSVC_INT_CAST (int)
84#else
85#define MSVC_INT_CAST
86#endif
87
88#include <stdio.h>
89
Jerome Forissier039e02d2022-08-09 17:10:15 +020090#if defined(MBEDTLS_HAVE_TIME)
Jens Wiklander817466c2018-05-22 13:49:31 +020091#include <time.h>
Jerome Forissier039e02d2022-08-09 17:10:15 +020092#endif
Jens Wiklander817466c2018-05-22 13:49:31 +020093
94#include <stdint.h>
95
96/*
97 * Prepare for using the sockets interface
98 */
Jens Wiklander32b31802023-10-06 16:59:46 +020099static int net_prepare(void)
Jens Wiklander817466c2018-05-22 13:49:31 +0200100{
Jens Wiklander32b31802023-10-06 16:59:46 +0200101#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Jens Wiklander817466c2018-05-22 13:49:31 +0200102 !defined(EFI32)
103 WSADATA wsaData;
104
Jens Wiklander32b31802023-10-06 16:59:46 +0200105 if (wsa_init_done == 0) {
106 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
107 return MBEDTLS_ERR_NET_SOCKET_FAILED;
108 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200109
110 wsa_init_done = 1;
111 }
112#else
113#if !defined(EFIX64) && !defined(EFI32)
Jens Wiklander32b31802023-10-06 16:59:46 +0200114 signal(SIGPIPE, SIG_IGN);
Jens Wiklander817466c2018-05-22 13:49:31 +0200115#endif
116#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200117 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200118}
119
120/*
Jerome Forissier79013242021-07-28 10:24:04 +0200121 * Return 0 if the file descriptor is valid, an error otherwise.
122 * If for_select != 0, check whether the file descriptor is within the range
123 * allowed for fd_set used for the FD_xxx macros and the select() function.
124 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200125static int check_fd(int fd, int for_select)
Jerome Forissier79013242021-07-28 10:24:04 +0200126{
Jens Wiklander32b31802023-10-06 16:59:46 +0200127 if (fd < 0) {
128 return MBEDTLS_ERR_NET_INVALID_CONTEXT;
129 }
Jerome Forissier79013242021-07-28 10:24:04 +0200130
131#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
132 !defined(EFI32)
133 (void) for_select;
134#else
135 /* A limitation of select() is that it only works with file descriptors
136 * that are strictly less than FD_SETSIZE. This is a limitation of the
137 * fd_set type. Error out early, because attempting to call FD_SET on a
138 * large file descriptor is a buffer overflow on typical platforms. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200139 if (for_select && fd >= FD_SETSIZE) {
140 return MBEDTLS_ERR_NET_POLL_FAILED;
141 }
Jerome Forissier79013242021-07-28 10:24:04 +0200142#endif
143
Jens Wiklander32b31802023-10-06 16:59:46 +0200144 return 0;
Jerome Forissier79013242021-07-28 10:24:04 +0200145}
146
147/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200148 * Initialize a context
149 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200150void mbedtls_net_init(mbedtls_net_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200151{
152 ctx->fd = -1;
153}
154
155/*
156 * Initiate a TCP connection with host:port and the given protocol
157 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200158int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
159 const char *port, int proto)
Jens Wiklander817466c2018-05-22 13:49:31 +0200160{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200162 struct addrinfo hints, *addr_list, *cur;
163
Jens Wiklander32b31802023-10-06 16:59:46 +0200164 if ((ret = net_prepare()) != 0) {
165 return ret;
166 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200167
168 /* Do name resolution with both IPv6 and IPv4 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200169 memset(&hints, 0, sizeof(hints));
Jens Wiklander817466c2018-05-22 13:49:31 +0200170 hints.ai_family = AF_UNSPEC;
171 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
172 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
173
Jens Wiklander32b31802023-10-06 16:59:46 +0200174 if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
175 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
176 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200177
178 /* Try the sockaddrs until a connection succeeds */
179 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Jens Wiklander32b31802023-10-06 16:59:46 +0200180 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
181 ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
182 cur->ai_protocol);
183 if (ctx->fd < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200184 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
185 continue;
186 }
187
Jens Wiklander32b31802023-10-06 16:59:46 +0200188 if (connect(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) == 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200189 ret = 0;
190 break;
191 }
192
Jens Wiklander32b31802023-10-06 16:59:46 +0200193 close(ctx->fd);
Jens Wiklander817466c2018-05-22 13:49:31 +0200194 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
195 }
196
Jens Wiklander32b31802023-10-06 16:59:46 +0200197 freeaddrinfo(addr_list);
Jens Wiklander817466c2018-05-22 13:49:31 +0200198
Jens Wiklander32b31802023-10-06 16:59:46 +0200199 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200200}
201
202/*
203 * Create a listening socket on bind_ip:port
204 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200205int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto)
Jens Wiklander817466c2018-05-22 13:49:31 +0200206{
207 int n, ret;
208 struct addrinfo hints, *addr_list, *cur;
209
Jens Wiklander32b31802023-10-06 16:59:46 +0200210 if ((ret = net_prepare()) != 0) {
211 return ret;
212 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200213
214 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Jens Wiklander32b31802023-10-06 16:59:46 +0200215 memset(&hints, 0, sizeof(hints));
Jens Wiklander817466c2018-05-22 13:49:31 +0200216 hints.ai_family = AF_UNSPEC;
217 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
218 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Jens Wiklander32b31802023-10-06 16:59:46 +0200219 if (bind_ip == NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200220 hints.ai_flags = AI_PASSIVE;
Jens Wiklander32b31802023-10-06 16:59:46 +0200221 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200222
Jens Wiklander32b31802023-10-06 16:59:46 +0200223 if (getaddrinfo(bind_ip, port, &hints, &addr_list) != 0) {
224 return MBEDTLS_ERR_NET_UNKNOWN_HOST;
225 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200226
227 /* Try the sockaddrs until a binding succeeds */
228 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Jens Wiklander32b31802023-10-06 16:59:46 +0200229 for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
230 ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
231 cur->ai_protocol);
232 if (ctx->fd < 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200233 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
234 continue;
235 }
236
237 n = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200238 if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
239 (const char *) &n, sizeof(n)) != 0) {
240 close(ctx->fd);
Jens Wiklander817466c2018-05-22 13:49:31 +0200241 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
242 continue;
243 }
244
Jens Wiklander32b31802023-10-06 16:59:46 +0200245 if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
246 close(ctx->fd);
Jens Wiklander817466c2018-05-22 13:49:31 +0200247 ret = MBEDTLS_ERR_NET_BIND_FAILED;
248 continue;
249 }
250
251 /* Listen only makes sense for TCP */
Jens Wiklander32b31802023-10-06 16:59:46 +0200252 if (proto == MBEDTLS_NET_PROTO_TCP) {
253 if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
254 close(ctx->fd);
Jens Wiklander817466c2018-05-22 13:49:31 +0200255 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
256 continue;
257 }
258 }
259
260 /* Bind was successful */
261 ret = 0;
262 break;
263 }
264
Jens Wiklander32b31802023-10-06 16:59:46 +0200265 freeaddrinfo(addr_list);
Jens Wiklander817466c2018-05-22 13:49:31 +0200266
Jens Wiklander32b31802023-10-06 16:59:46 +0200267 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200268
269}
270
Jens Wiklander32b31802023-10-06 16:59:46 +0200271#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Jens Wiklander817466c2018-05-22 13:49:31 +0200272 !defined(EFI32)
273/*
274 * Check if the requested operation would be blocking on a non-blocking socket
275 * and thus 'failed' with a negative return value.
276 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200277static int net_would_block(const mbedtls_net_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200278{
279 ((void) ctx);
Jens Wiklander32b31802023-10-06 16:59:46 +0200280 return WSAGetLastError() == WSAEWOULDBLOCK;
Jens Wiklander817466c2018-05-22 13:49:31 +0200281}
282#else
283/*
284 * Check if the requested operation would be blocking on a non-blocking socket
285 * and thus 'failed' with a negative return value.
286 *
287 * Note: on a blocking socket this function always returns 0!
288 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200289static int net_would_block(const mbedtls_net_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200290{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100291 int err = errno;
292
Jens Wiklander817466c2018-05-22 13:49:31 +0200293 /*
Jerome Forissier5b25c762020-04-07 11:18:49 +0200294 * Never return 'WOULD BLOCK' on a blocking socket
Jens Wiklander817466c2018-05-22 13:49:31 +0200295 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200296 if ((fcntl(ctx->fd, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100297 errno = err;
Jens Wiklander32b31802023-10-06 16:59:46 +0200298 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100299 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200300
Jens Wiklander32b31802023-10-06 16:59:46 +0200301 switch (errno = err) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200302#if defined EAGAIN
303 case EAGAIN:
304#endif
305#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
306 case EWOULDBLOCK:
307#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200308 return 1;
Jens Wiklander817466c2018-05-22 13:49:31 +0200309 }
Jens Wiklander32b31802023-10-06 16:59:46 +0200310 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200311}
312#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
313
314/*
315 * Accept a connection from a remote client
316 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200317int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
318 mbedtls_net_context *client_ctx,
Tom Van Eyckc1633172024-04-09 18:44:13 +0200319 void *client_ip, size_t buf_size, size_t *cip_len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200320{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200322 int type;
323
324 struct sockaddr_storage client_addr;
325
326#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
Jerome Forissier79013242021-07-28 10:24:04 +0200327 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
328 defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
Jens Wiklander32b31802023-10-06 16:59:46 +0200329 socklen_t n = (socklen_t) sizeof(client_addr);
330 socklen_t type_len = (socklen_t) sizeof(type);
Jens Wiklander817466c2018-05-22 13:49:31 +0200331#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200332 int n = (int) sizeof(client_addr);
333 int type_len = (int) sizeof(type);
Jens Wiklander817466c2018-05-22 13:49:31 +0200334#endif
335
336 /* Is this a TCP or UDP socket? */
Jens Wiklander32b31802023-10-06 16:59:46 +0200337 if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE,
338 (void *) &type, &type_len) != 0 ||
339 (type != SOCK_STREAM && type != SOCK_DGRAM)) {
340 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200341 }
342
Jens Wiklander32b31802023-10-06 16:59:46 +0200343 if (type == SOCK_STREAM) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200344 /* TCP: actual accept() */
Jens Wiklander32b31802023-10-06 16:59:46 +0200345 ret = client_ctx->fd = (int) accept(bind_ctx->fd,
346 (struct sockaddr *) &client_addr, &n);
347 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +0200348 /* UDP: wait for a message, but keep it in the queue */
349 char buf[1] = { 0 };
350
Jens Wiklander32b31802023-10-06 16:59:46 +0200351 ret = (int) recvfrom(bind_ctx->fd, buf, sizeof(buf), MSG_PEEK,
352 (struct sockaddr *) &client_addr, &n);
Jens Wiklander817466c2018-05-22 13:49:31 +0200353
354#if defined(_WIN32)
Jens Wiklander32b31802023-10-06 16:59:46 +0200355 if (ret == SOCKET_ERROR &&
356 WSAGetLastError() == WSAEMSGSIZE) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200357 /* We know buf is too small, thanks, just peeking here */
358 ret = 0;
359 }
360#endif
361 }
362
Jens Wiklander32b31802023-10-06 16:59:46 +0200363 if (ret < 0) {
364 if (net_would_block(bind_ctx) != 0) {
365 return MBEDTLS_ERR_SSL_WANT_READ;
366 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200367
Jens Wiklander32b31802023-10-06 16:59:46 +0200368 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200369 }
370
371 /* UDP: hijack the listening socket to communicate with the client,
372 * then bind a new socket to accept new connections */
Jens Wiklander32b31802023-10-06 16:59:46 +0200373 if (type != SOCK_STREAM) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200374 struct sockaddr_storage local_addr;
375 int one = 1;
376
Jens Wiklander32b31802023-10-06 16:59:46 +0200377 if (connect(bind_ctx->fd, (struct sockaddr *) &client_addr, n) != 0) {
378 return MBEDTLS_ERR_NET_ACCEPT_FAILED;
379 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200380
381 client_ctx->fd = bind_ctx->fd;
382 bind_ctx->fd = -1; /* In case we exit early */
383
Jens Wiklander32b31802023-10-06 16:59:46 +0200384 n = sizeof(struct sockaddr_storage);
385 if (getsockname(client_ctx->fd,
386 (struct sockaddr *) &local_addr, &n) != 0 ||
387 (bind_ctx->fd = (int) socket(local_addr.ss_family,
388 SOCK_DGRAM, IPPROTO_UDP)) < 0 ||
389 setsockopt(bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
390 (const char *) &one, sizeof(one)) != 0) {
391 return MBEDTLS_ERR_NET_SOCKET_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200392 }
393
Jens Wiklander32b31802023-10-06 16:59:46 +0200394 if (bind(bind_ctx->fd, (struct sockaddr *) &local_addr, n) != 0) {
395 return MBEDTLS_ERR_NET_BIND_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200396 }
397 }
398
Jens Wiklander32b31802023-10-06 16:59:46 +0200399 if (client_ip != NULL) {
400 if (client_addr.ss_family == AF_INET) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200401 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200402 *cip_len = sizeof(addr4->sin_addr.s_addr);
Jens Wiklander817466c2018-05-22 13:49:31 +0200403
Tom Van Eyckc1633172024-04-09 18:44:13 +0200404 if (buf_size < *cip_len) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200405 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
406 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200407
Tom Van Eyckc1633172024-04-09 18:44:13 +0200408 memcpy(client_ip, &addr4->sin_addr.s_addr, *cip_len);
Jens Wiklander32b31802023-10-06 16:59:46 +0200409 } else {
Jens Wiklander817466c2018-05-22 13:49:31 +0200410 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200411 *cip_len = sizeof(addr6->sin6_addr.s6_addr);
Jens Wiklander817466c2018-05-22 13:49:31 +0200412
Tom Van Eyckc1633172024-04-09 18:44:13 +0200413 if (buf_size < *cip_len) {
Jens Wiklander32b31802023-10-06 16:59:46 +0200414 return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
415 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200416
Tom Van Eyckc1633172024-04-09 18:44:13 +0200417 memcpy(client_ip, &addr6->sin6_addr.s6_addr, *cip_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200418 }
419 }
420
Jens Wiklander32b31802023-10-06 16:59:46 +0200421 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200422}
423
424/*
425 * Set the socket blocking or non-blocking
426 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200427int mbedtls_net_set_block(mbedtls_net_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200428{
Jens Wiklander32b31802023-10-06 16:59:46 +0200429#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Jens Wiklander817466c2018-05-22 13:49:31 +0200430 !defined(EFI32)
431 u_long n = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200432 return ioctlsocket(ctx->fd, FIONBIO, &n);
Jens Wiklander817466c2018-05-22 13:49:31 +0200433#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200434 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) & ~O_NONBLOCK);
Jens Wiklander817466c2018-05-22 13:49:31 +0200435#endif
436}
437
Jens Wiklander32b31802023-10-06 16:59:46 +0200438int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200439{
Jens Wiklander32b31802023-10-06 16:59:46 +0200440#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
Jens Wiklander817466c2018-05-22 13:49:31 +0200441 !defined(EFI32)
442 u_long n = 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200443 return ioctlsocket(ctx->fd, FIONBIO, &n);
Jens Wiklander817466c2018-05-22 13:49:31 +0200444#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200445 return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) | O_NONBLOCK);
Jens Wiklander817466c2018-05-22 13:49:31 +0200446#endif
447}
448
449/*
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100450 * Check if data is available on the socket
451 */
452
Jens Wiklander32b31802023-10-06 16:59:46 +0200453int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100454{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100456 struct timeval tv;
457
458 fd_set read_fds;
459 fd_set write_fds;
460
461 int fd = ctx->fd;
462
Jens Wiklander32b31802023-10-06 16:59:46 +0200463 ret = check_fd(fd, 1);
464 if (ret != 0) {
465 return ret;
466 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100467
468#if defined(__has_feature)
469#if __has_feature(memory_sanitizer)
470 /* Ensure that memory sanitizers consider read_fds and write_fds as
471 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
472 * is implemented in assembly. */
Jens Wiklander32b31802023-10-06 16:59:46 +0200473 memset(&read_fds, 0, sizeof(read_fds));
474 memset(&write_fds, 0, sizeof(write_fds));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100475#endif
476#endif
477
Jens Wiklander32b31802023-10-06 16:59:46 +0200478 FD_ZERO(&read_fds);
479 if (rw & MBEDTLS_NET_POLL_READ) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100480 rw &= ~MBEDTLS_NET_POLL_READ;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200481 FD_SET((SOCKET) fd, &read_fds);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100482 }
483
Jens Wiklander32b31802023-10-06 16:59:46 +0200484 FD_ZERO(&write_fds);
485 if (rw & MBEDTLS_NET_POLL_WRITE) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100486 rw &= ~MBEDTLS_NET_POLL_WRITE;
Tom Van Eyckc1633172024-04-09 18:44:13 +0200487 FD_SET((SOCKET) fd, &write_fds);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100488 }
489
Jens Wiklander32b31802023-10-06 16:59:46 +0200490 if (rw != 0) {
491 return MBEDTLS_ERR_NET_BAD_INPUT_DATA;
492 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100493
494 tv.tv_sec = timeout / 1000;
Jens Wiklander32b31802023-10-06 16:59:46 +0200495 tv.tv_usec = (timeout % 1000) * 1000;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100496
Jens Wiklander32b31802023-10-06 16:59:46 +0200497 do {
498 ret = select(fd + 1, &read_fds, &write_fds, NULL,
499 timeout == (uint32_t) -1 ? NULL : &tv);
500 } while (IS_EINTR(ret));
501
502 if (ret < 0) {
503 return MBEDTLS_ERR_NET_POLL_FAILED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100504 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100505
506 ret = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200507 if (FD_ISSET(fd, &read_fds)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100508 ret |= MBEDTLS_NET_POLL_READ;
Jens Wiklander32b31802023-10-06 16:59:46 +0200509 }
510 if (FD_ISSET(fd, &write_fds)) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100511 ret |= MBEDTLS_NET_POLL_WRITE;
Jens Wiklander32b31802023-10-06 16:59:46 +0200512 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100513
Jens Wiklander32b31802023-10-06 16:59:46 +0200514 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100515}
516
517/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200518 * Portable usleep helper
519 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200520void mbedtls_net_usleep(unsigned long usec)
Jens Wiklander817466c2018-05-22 13:49:31 +0200521{
522#if defined(_WIN32)
Jens Wiklander32b31802023-10-06 16:59:46 +0200523 Sleep((usec + 999) / 1000);
Jens Wiklander817466c2018-05-22 13:49:31 +0200524#else
525 struct timeval tv;
526 tv.tv_sec = usec / 1000000;
527#if defined(__unix__) || defined(__unix) || \
Jens Wiklander32b31802023-10-06 16:59:46 +0200528 (defined(__APPLE__) && defined(__MACH__))
Jens Wiklander817466c2018-05-22 13:49:31 +0200529 tv.tv_usec = (suseconds_t) usec % 1000000;
530#else
531 tv.tv_usec = usec % 1000000;
532#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200533 select(0, NULL, NULL, NULL, &tv);
Jens Wiklander817466c2018-05-22 13:49:31 +0200534#endif
535}
536
537/*
538 * Read at most 'len' characters
539 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200540int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200541{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200543 int fd = ((mbedtls_net_context *) ctx)->fd;
544
Jens Wiklander32b31802023-10-06 16:59:46 +0200545 ret = check_fd(fd, 0);
546 if (ret != 0) {
547 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200548 }
549
Jens Wiklander32b31802023-10-06 16:59:46 +0200550 ret = (int) read(fd, buf, len);
551
552 if (ret < 0) {
553 if (net_would_block(ctx) != 0) {
554 return MBEDTLS_ERR_SSL_WANT_READ;
555 }
556
557#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
558 !defined(EFI32)
559 if (WSAGetLastError() == WSAECONNRESET) {
560 return MBEDTLS_ERR_NET_CONN_RESET;
561 }
562#else
563 if (errno == EPIPE || errno == ECONNRESET) {
564 return MBEDTLS_ERR_NET_CONN_RESET;
565 }
566
567 if (errno == EINTR) {
568 return MBEDTLS_ERR_SSL_WANT_READ;
569 }
570#endif
571
572 return MBEDTLS_ERR_NET_RECV_FAILED;
573 }
574
575 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200576}
577
578/*
579 * Read at most 'len' characters, blocking for at most 'timeout' ms
580 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200581int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf,
582 size_t len, uint32_t timeout)
Jens Wiklander817466c2018-05-22 13:49:31 +0200583{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200585 struct timeval tv;
586 fd_set read_fds;
587 int fd = ((mbedtls_net_context *) ctx)->fd;
588
Jens Wiklander32b31802023-10-06 16:59:46 +0200589 ret = check_fd(fd, 1);
590 if (ret != 0) {
591 return ret;
592 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200593
Jens Wiklander32b31802023-10-06 16:59:46 +0200594 FD_ZERO(&read_fds);
Tom Van Eyckc1633172024-04-09 18:44:13 +0200595 FD_SET((SOCKET) fd, &read_fds);
Jens Wiklander817466c2018-05-22 13:49:31 +0200596
597 tv.tv_sec = timeout / 1000;
Jens Wiklander32b31802023-10-06 16:59:46 +0200598 tv.tv_usec = (timeout % 1000) * 1000;
Jens Wiklander817466c2018-05-22 13:49:31 +0200599
Jens Wiklander32b31802023-10-06 16:59:46 +0200600 ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
Jens Wiklander817466c2018-05-22 13:49:31 +0200601
602 /* Zero fds ready means we timed out */
Jens Wiklander32b31802023-10-06 16:59:46 +0200603 if (ret == 0) {
604 return MBEDTLS_ERR_SSL_TIMEOUT;
605 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200606
Jens Wiklander32b31802023-10-06 16:59:46 +0200607 if (ret < 0) {
608#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
609 !defined(EFI32)
610 if (WSAGetLastError() == WSAEINTR) {
611 return MBEDTLS_ERR_SSL_WANT_READ;
612 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200613#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200614 if (errno == EINTR) {
615 return MBEDTLS_ERR_SSL_WANT_READ;
616 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200617#endif
618
Jens Wiklander32b31802023-10-06 16:59:46 +0200619 return MBEDTLS_ERR_NET_RECV_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200620 }
621
622 /* This call will not block */
Jens Wiklander32b31802023-10-06 16:59:46 +0200623 return mbedtls_net_recv(ctx, buf, len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200624}
625
626/*
627 * Write at most 'len' characters
628 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200629int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200630{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200632 int fd = ((mbedtls_net_context *) ctx)->fd;
633
Jens Wiklander32b31802023-10-06 16:59:46 +0200634 ret = check_fd(fd, 0);
635 if (ret != 0) {
636 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200637 }
638
Jens Wiklander32b31802023-10-06 16:59:46 +0200639 ret = (int) write(fd, buf, len);
640
641 if (ret < 0) {
642 if (net_would_block(ctx) != 0) {
643 return MBEDTLS_ERR_SSL_WANT_WRITE;
644 }
645
646#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
647 !defined(EFI32)
648 if (WSAGetLastError() == WSAECONNRESET) {
649 return MBEDTLS_ERR_NET_CONN_RESET;
650 }
651#else
652 if (errno == EPIPE || errno == ECONNRESET) {
653 return MBEDTLS_ERR_NET_CONN_RESET;
654 }
655
656 if (errno == EINTR) {
657 return MBEDTLS_ERR_SSL_WANT_WRITE;
658 }
659#endif
660
661 return MBEDTLS_ERR_NET_SEND_FAILED;
662 }
663
664 return ret;
Jens Wiklander817466c2018-05-22 13:49:31 +0200665}
666
667/*
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200668 * Close the connection
669 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200670void mbedtls_net_close(mbedtls_net_context *ctx)
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200671{
Jens Wiklander32b31802023-10-06 16:59:46 +0200672 if (ctx->fd == -1) {
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200673 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200674 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200675
Jens Wiklander32b31802023-10-06 16:59:46 +0200676 close(ctx->fd);
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200677
678 ctx->fd = -1;
679}
680
681/*
Jens Wiklander817466c2018-05-22 13:49:31 +0200682 * Gracefully close the connection
683 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200684void mbedtls_net_free(mbedtls_net_context *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200685{
Jens Wiklander32b31802023-10-06 16:59:46 +0200686 if (ctx->fd == -1) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200687 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200688 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200689
Jens Wiklander32b31802023-10-06 16:59:46 +0200690 shutdown(ctx->fd, 2);
691 close(ctx->fd);
Jens Wiklander817466c2018-05-22 13:49:31 +0200692
693 ctx->fd = -1;
694}
695
696#endif /* MBEDTLS_NET_C */