2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2014-09-17 10:56:54 +02:00
|
|
|
* TCP/IP or UDP/IP networking functions
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2009-01-04 17:27:10 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
|
2017-12-05 13:08:15 +01:00
|
|
|
/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
|
2021-05-28 09:42:25 +02:00
|
|
|
* be set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
|
2017-12-05 13:08:15 +01:00
|
|
|
* Harmless on other platforms. */
|
2020-05-28 07:41:19 +02:00
|
|
|
#ifndef _POSIX_C_SOURCE
|
2017-12-05 13:08:15 +01:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2020-05-28 07:41:19 +02:00
|
|
|
#endif
|
|
|
|
#ifndef _XOPEN_SOURCE
|
2020-06-11 13:29:15 +02:00
|
|
|
#define _XOPEN_SOURCE 600 /* sockaddr_storage */
|
2020-05-28 07:41:19 +02:00
|
|
|
#endif
|
2017-12-05 13:08:15 +01:00
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_NET_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2016-02-22 10:33:34 +01:00
|
|
|
#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
|
2018-04-12 02:27:32 +02:00
|
|
|
!defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
|
2020-05-20 11:32:39 +02:00
|
|
|
!defined(__HAIKU__) && !defined(__midipix__)
|
2021-05-28 09:42:25 +02:00
|
|
|
#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in mbedtls_config.h"
|
2016-02-22 10:33:34 +01:00
|
|
|
#endif
|
|
|
|
|
2016-04-26 08:43:27 +02:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2016-09-14 15:32:09 +02:00
|
|
|
#include "mbedtls/net_sockets.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#include "mbedtls/error.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-28 18:48:30 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define IS_EINTR(ret) ((ret) == WSAEINTR)
|
2018-03-15 16:49:24 +01:00
|
|
|
|
2013-12-17 16:10:58 +01:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
2013-12-17 17:38:55 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
2020-04-24 16:00:36 +02:00
|
|
|
#if (_WIN32_WINNT < 0x0501)
|
|
|
|
#include <wspiapi.h>
|
|
|
|
#endif
|
2013-12-17 17:38:55 +01:00
|
|
|
|
2013-12-30 15:42:43 +01:00
|
|
|
#if defined(_MSC_VER)
|
2009-01-03 22:22:43 +01:00
|
|
|
#if defined(_WIN32_WCE)
|
|
|
|
#pragma comment( lib, "ws2.lib" )
|
|
|
|
#else
|
|
|
|
#pragma comment( lib, "ws2_32.lib" )
|
|
|
|
#endif
|
2013-12-30 15:42:43 +01:00
|
|
|
#endif /* _MSC_VER */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define read(fd, buf, len) recv(fd, (char *) (buf), (int) (len), 0)
|
|
|
|
#define write(fd, buf, len) send(fd, (char *) (buf), (int) (len), 0)
|
2009-01-03 22:22:43 +01:00
|
|
|
#define close(fd) closesocket(fd)
|
|
|
|
|
|
|
|
static int wsa_init_done = 0;
|
|
|
|
|
2014-06-17 14:34:44 +02:00
|
|
|
#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
2009-07-27 23:09:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define IS_EINTR(ret) ((ret) == EINTR)
|
2023-03-06 23:51:39 +01:00
|
|
|
#define SOCKET int
|
2018-03-15 16:49:24 +01:00
|
|
|
|
2014-06-17 14:34:44 +02:00
|
|
|
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-07-01 15:51:43 +02:00
|
|
|
/* Some MS functions want int and MSVC warns if we pass size_t,
|
2017-07-12 15:04:40 +02:00
|
|
|
* but the standard functions use socklen_t, so cast only for MSVC */
|
2015-07-01 15:51:43 +02:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define MSVC_INT_CAST (int)
|
|
|
|
#else
|
|
|
|
#define MSVC_INT_CAST
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <stdio.h>
|
2013-07-03 15:31:03 +02:00
|
|
|
|
2022-03-02 16:55:08 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2009-01-03 22:22:43 +01:00
|
|
|
#include <time.h>
|
2022-03-02 16:55:08 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-06-22 19:21:23 +02:00
|
|
|
#include <stdint.h>
|
2012-10-01 16:41:15 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2013-12-13 11:55:32 +01:00
|
|
|
* Prepare for using the sockets interface
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int net_prepare(void)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
2013-10-28 18:48:30 +01:00
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
WSADATA wsaData;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (wsa_init_done == 0) {
|
|
|
|
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_SOCKET_FAILED;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
wsa_init_done = 1;
|
|
|
|
}
|
|
|
|
#else
|
2013-10-28 18:48:30 +01:00
|
|
|
#if !defined(EFIX64) && !defined(EFI32)
|
2023-01-11 14:50:10 +01:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2013-10-28 18:48:30 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-12-13 11:55:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-20 23:08:19 +02:00
|
|
|
/*
|
|
|
|
* Return 0 if the file descriptor is valid, an error otherwise.
|
|
|
|
* If for_select != 0, check whether the file descriptor is within the range
|
|
|
|
* allowed for fd_set used for the FD_xxx macros and the select() function.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int check_fd(int fd, int for_select)
|
2021-06-20 23:08:19 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
|
|
|
|
}
|
2021-06-20 23:08:19 +02:00
|
|
|
|
2021-06-20 22:01:36 +02:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
(void) for_select;
|
|
|
|
#else
|
2021-06-20 23:08:19 +02:00
|
|
|
/* A limitation of select() is that it only works with file descriptors
|
|
|
|
* that are strictly less than FD_SETSIZE. This is a limitation of the
|
|
|
|
* fd_set type. Error out early, because attempting to call FD_SET on a
|
|
|
|
* large file descriptor is a buffer overflow on typical platforms. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (for_select && fd >= FD_SETSIZE) {
|
|
|
|
return MBEDTLS_ERR_NET_POLL_FAILED;
|
|
|
|
}
|
2021-06-20 22:01:36 +02:00
|
|
|
#endif
|
2021-06-20 23:08:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-06-20 23:08:19 +02:00
|
|
|
}
|
|
|
|
|
2015-06-30 13:34:45 +02:00
|
|
|
/*
|
|
|
|
* Initialize a context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_net_init(mbedtls_net_context *ctx)
|
2015-06-30 13:34:45 +02:00
|
|
|
{
|
|
|
|
ctx->fd = -1;
|
|
|
|
}
|
|
|
|
|
2013-12-13 11:55:32 +01:00
|
|
|
/*
|
2014-03-23 17:38:16 +01:00
|
|
|
* Initiate a TCP connection with host:port and the given protocol
|
2013-12-13 11:55:32 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host,
|
|
|
|
const char *port, int proto)
|
2013-12-13 11:55:32 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-12-13 12:54:09 +01:00
|
|
|
struct addrinfo hints, *addr_list, *cur;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = net_prepare()) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-13 12:54:09 +01:00
|
|
|
|
2014-03-23 17:38:16 +01:00
|
|
|
/* Do name resolution with both IPv6 and IPv4 */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2013-12-13 12:54:09 +01:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2015-04-08 12:49:31 +02:00
|
|
|
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
|
2013-12-13 12:54:09 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
|
|
|
}
|
2013-12-13 12:54:09 +01:00
|
|
|
|
|
|
|
/* Try the sockaddrs until a connection succeeds */
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
|
|
|
|
ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
|
|
|
|
cur->ai_protocol);
|
|
|
|
if (ctx->fd < 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2013-12-13 12:54:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (connect(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) == 0) {
|
2013-12-13 12:54:09 +01:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
close(ctx->fd);
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
|
2013-12-13 12:54:09 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
freeaddrinfo(addr_list);
|
2013-12-13 12:54:09 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a listening socket on bind_ip:port
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-12-17 15:57:05 +01:00
|
|
|
int n, ret;
|
2013-12-13 14:00:51 +01:00
|
|
|
struct addrinfo hints, *addr_list, *cur;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = net_prepare()) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-12-13 14:00:51 +01:00
|
|
|
|
2015-06-30 17:19:48 +02:00
|
|
|
/* Bind to IPv6 and/or IPv4, but only in the desired protocol */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2013-12-13 14:00:51 +01:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2015-04-08 12:49:31 +02:00
|
|
|
hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (bind_ip == NULL) {
|
2013-12-13 14:00:51 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-12-13 14:00:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (getaddrinfo(bind_ip, port, &hints, &addr_list) != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
|
|
|
}
|
2013-12-13 14:00:51 +01:00
|
|
|
|
|
|
|
/* Try the sockaddrs until a binding succeeds */
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = addr_list; cur != NULL; cur = cur->ai_next) {
|
|
|
|
ctx->fd = (int) socket(cur->ai_family, cur->ai_socktype,
|
|
|
|
cur->ai_protocol);
|
|
|
|
if (ctx->fd < 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2013-12-13 14:00:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-17 13:59:01 +01:00
|
|
|
n = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (setsockopt(ctx->fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const char *) &n, sizeof(n)) != 0) {
|
|
|
|
close(ctx->fd);
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2014-04-17 12:43:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-17 13:59:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (bind(ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen) != 0) {
|
|
|
|
close(ctx->fd);
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_BIND_FAILED;
|
2013-12-13 14:00:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-23 17:38:16 +01:00
|
|
|
/* Listen only makes sense for TCP */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (proto == MBEDTLS_NET_PROTO_TCP) {
|
|
|
|
if (listen(ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG) != 0) {
|
|
|
|
close(ctx->fd);
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
|
2014-03-23 17:38:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-13 14:00:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-06 13:45:15 +01:00
|
|
|
/* Bind was successful */
|
2013-12-13 14:00:51 +01:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
freeaddrinfo(addr_list);
|
2013-12-13 14:00:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-12-13 14:00:51 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
2014-01-23 20:59:49 +01:00
|
|
|
!defined(EFI32)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2014-01-23 20:59:49 +01:00
|
|
|
* Check if the requested operation would be blocking on a non-blocking socket
|
|
|
|
* and thus 'failed' with a negative return value.
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int net_would_block(const mbedtls_net_context *ctx)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-06-30 13:34:45 +02:00
|
|
|
((void) ctx);
|
2023-01-11 14:50:10 +01:00
|
|
|
return WSAGetLastError() == WSAEWOULDBLOCK;
|
2014-01-23 20:59:49 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
2014-01-23 20:59:49 +01:00
|
|
|
/*
|
|
|
|
* Check if the requested operation would be blocking on a non-blocking socket
|
|
|
|
* and thus 'failed' with a negative return value.
|
|
|
|
*
|
|
|
|
* Note: on a blocking socket this function always returns 0!
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int net_would_block(const mbedtls_net_context *ctx)
|
2014-01-23 20:59:49 +01:00
|
|
|
{
|
2017-05-04 14:39:22 +02:00
|
|
|
int err = errno;
|
2018-04-03 17:28:09 +02:00
|
|
|
|
2014-01-23 20:59:49 +01:00
|
|
|
/*
|
2019-05-29 10:38:29 +02:00
|
|
|
* Never return 'WOULD BLOCK' on a blocking socket
|
2014-01-23 20:59:49 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((fcntl(ctx->fd, F_GETFL) & O_NONBLOCK) != O_NONBLOCK) {
|
2017-05-04 14:39:22 +02:00
|
|
|
errno = err;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2017-05-04 14:39:22 +02:00
|
|
|
}
|
2014-01-23 20:59:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (errno = err) {
|
2009-01-03 22:22:43 +01:00
|
|
|
#if defined EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
#endif
|
|
|
|
#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2014-06-17 14:34:44 +02:00
|
|
|
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Accept a connection from a remote client
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
|
|
|
|
mbedtls_net_context *client_ctx,
|
|
|
|
void *client_ip, size_t buf_size, size_t *ip_len)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-03-23 17:38:16 +01:00
|
|
|
int type;
|
|
|
|
|
2013-12-13 16:21:25 +01:00
|
|
|
struct sockaddr_storage client_addr;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2011-12-20 13:19:03 +01:00
|
|
|
#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
|
2020-06-11 13:29:15 +02:00
|
|
|
defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
|
2020-09-03 09:36:36 +02:00
|
|
|
defined(socklen_t) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L)
|
2023-01-11 14:50:10 +01:00
|
|
|
socklen_t n = (socklen_t) sizeof(client_addr);
|
|
|
|
socklen_t type_len = (socklen_t) sizeof(type);
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
int n = (int) sizeof(client_addr);
|
|
|
|
int type_len = (int) sizeof(type);
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2014-03-23 17:38:16 +01:00
|
|
|
/* Is this a TCP or UDP socket? */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (getsockopt(bind_ctx->fd, SOL_SOCKET, SO_TYPE,
|
|
|
|
(void *) &type, &type_len) != 0 ||
|
|
|
|
(type != SOCK_STREAM && type != SOCK_DGRAM)) {
|
|
|
|
return MBEDTLS_ERR_NET_ACCEPT_FAILED;
|
2014-03-23 17:38:16 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (type == SOCK_STREAM) {
|
2014-03-23 17:38:16 +01:00
|
|
|
/* TCP: actual accept() */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = client_ctx->fd = (int) accept(bind_ctx->fd,
|
|
|
|
(struct sockaddr *) &client_addr, &n);
|
|
|
|
} else {
|
2014-03-23 17:38:16 +01:00
|
|
|
/* UDP: wait for a message, but keep it in the queue */
|
|
|
|
char buf[1] = { 0 };
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = (int) recvfrom(bind_ctx->fd, buf, sizeof(buf), MSG_PEEK,
|
|
|
|
(struct sockaddr *) &client_addr, &n);
|
2015-06-30 11:20:22 +02:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == SOCKET_ERROR &&
|
|
|
|
WSAGetLastError() == WSAEMSGSIZE) {
|
2015-06-30 11:20:22 +02:00
|
|
|
/* We know buf is too small, thanks, just peeking here */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-23 17:38:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
if (net_would_block(bind_ctx) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_NET_ACCEPT_FAILED;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-07-01 01:28:24 +02:00
|
|
|
/* UDP: hijack the listening socket to communicate with the client,
|
|
|
|
* then bind a new socket to accept new connections */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (type != SOCK_STREAM) {
|
2015-07-01 01:28:24 +02:00
|
|
|
struct sockaddr_storage local_addr;
|
|
|
|
int one = 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (connect(bind_ctx->fd, (struct sockaddr *) &client_addr, n) != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_ACCEPT_FAILED;
|
|
|
|
}
|
2014-03-23 17:38:16 +01:00
|
|
|
|
2015-06-30 13:34:45 +02:00
|
|
|
client_ctx->fd = bind_ctx->fd;
|
2015-07-01 01:28:24 +02:00
|
|
|
bind_ctx->fd = -1; /* In case we exit early */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
n = sizeof(struct sockaddr_storage);
|
|
|
|
if (getsockname(client_ctx->fd,
|
|
|
|
(struct sockaddr *) &local_addr, &n) != 0 ||
|
|
|
|
(bind_ctx->fd = (int) socket(local_addr.ss_family,
|
|
|
|
SOCK_DGRAM, IPPROTO_UDP)) < 0 ||
|
|
|
|
setsockopt(bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const char *) &one, sizeof(one)) != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_SOCKET_FAILED;
|
2015-07-01 01:28:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (bind(bind_ctx->fd, (struct sockaddr *) &local_addr, n) != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_BIND_FAILED;
|
2015-07-01 01:28:24 +02:00
|
|
|
}
|
2014-03-23 17:38:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (client_ip != NULL) {
|
|
|
|
if (client_addr.ss_family == AF_INET) {
|
2013-12-13 16:21:25 +01:00
|
|
|
struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
|
2023-01-11 14:50:10 +01:00
|
|
|
*ip_len = sizeof(addr4->sin_addr.s_addr);
|
2015-05-14 21:52:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_size < *ip_len) {
|
|
|
|
return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2015-05-14 21:52:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(client_ip, &addr4->sin_addr.s_addr, *ip_len);
|
|
|
|
} else {
|
2013-12-13 16:21:25 +01:00
|
|
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
|
2023-01-11 14:50:10 +01:00
|
|
|
*ip_len = sizeof(addr6->sin6_addr.s6_addr);
|
2015-05-14 21:52:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf_size < *ip_len) {
|
|
|
|
return MBEDTLS_ERR_NET_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2015-05-14 21:52:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
|
2013-12-13 16:21:25 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the socket blocking or non-blocking
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_set_block(mbedtls_net_context *ctx)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
2013-10-28 18:48:30 +01:00
|
|
|
!defined(EFI32)
|
2011-04-24 18:08:12 +02:00
|
|
|
u_long n = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ioctlsocket(ctx->fd, FIONBIO, &n);
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) & ~O_NONBLOCK);
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_set_nonblock(mbedtls_net_context *ctx)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
2013-10-28 18:48:30 +01:00
|
|
|
!defined(EFI32)
|
2011-04-24 18:08:12 +02:00
|
|
|
u_long n = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ioctlsocket(ctx->fd, FIONBIO, &n);
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return fcntl(ctx->fd, F_SETFL, fcntl(ctx->fd, F_GETFL) | O_NONBLOCK);
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:06:06 +02:00
|
|
|
/*
|
|
|
|
* Check if data is available on the socket
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_poll(mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout)
|
2017-05-22 16:06:06 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2017-05-22 16:06:06 +02:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
fd_set read_fds;
|
|
|
|
fd_set write_fds;
|
|
|
|
|
|
|
|
int fd = ctx->fd;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = check_fd(fd, 1);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-02-24 19:49:44 +01:00
|
|
|
|
2018-04-05 14:55:47 +02:00
|
|
|
#if defined(__has_feature)
|
|
|
|
#if __has_feature(memory_sanitizer)
|
|
|
|
/* Ensure that memory sanitizers consider read_fds and write_fds as
|
|
|
|
* initialized even on platforms such as Glibc/x86_64 where FD_ZERO
|
|
|
|
* is implemented in assembly. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&read_fds, 0, sizeof(read_fds));
|
|
|
|
memset(&write_fds, 0, sizeof(write_fds));
|
2018-04-05 14:55:47 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2018-04-03 17:28:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
if (rw & MBEDTLS_NET_POLL_READ) {
|
2017-05-22 16:06:06 +02:00
|
|
|
rw &= ~MBEDTLS_NET_POLL_READ;
|
2023-03-06 23:51:39 +01:00
|
|
|
FD_SET((SOCKET) fd, &read_fds);
|
2017-05-22 16:06:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
FD_ZERO(&write_fds);
|
|
|
|
if (rw & MBEDTLS_NET_POLL_WRITE) {
|
2017-05-22 16:06:06 +02:00
|
|
|
rw &= ~MBEDTLS_NET_POLL_WRITE;
|
2023-03-06 23:51:39 +01:00
|
|
|
FD_SET((SOCKET) fd, &write_fds);
|
2017-05-22 16:06:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rw != 0) {
|
|
|
|
return MBEDTLS_ERR_NET_BAD_INPUT_DATA;
|
|
|
|
}
|
2017-05-22 16:06:06 +02:00
|
|
|
|
|
|
|
tv.tv_sec = timeout / 1000;
|
2023-01-11 14:50:10 +01:00
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2017-05-22 16:06:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
ret = select(fd + 1, &read_fds, &write_fds, NULL,
|
|
|
|
timeout == (uint32_t) -1 ? NULL : &tv);
|
|
|
|
} while (IS_EINTR(ret));
|
2017-05-22 16:06:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return MBEDTLS_ERR_NET_POLL_FAILED;
|
|
|
|
}
|
2017-05-22 16:06:06 +02:00
|
|
|
|
|
|
|
ret = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (FD_ISSET(fd, &read_fds)) {
|
2017-05-22 16:06:06 +02:00
|
|
|
ret |= MBEDTLS_NET_POLL_READ;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (FD_ISSET(fd, &write_fds)) {
|
2017-05-22 16:06:06 +02:00
|
|
|
ret |= MBEDTLS_NET_POLL_WRITE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-05-22 16:06:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2017-05-22 16:06:06 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Portable usleep helper
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_net_usleep(unsigned long usec)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-07-01 11:29:31 +02:00
|
|
|
#if defined(_WIN32)
|
2023-01-11 14:50:10 +01:00
|
|
|
Sleep((usec + 999) / 1000);
|
2015-07-01 11:29:31 +02:00
|
|
|
#else
|
2009-01-03 22:22:43 +01:00
|
|
|
struct timeval tv;
|
2014-11-21 09:52:23 +01:00
|
|
|
tv.tv_sec = usec / 1000000;
|
2015-07-01 11:29:31 +02:00
|
|
|
#if defined(__unix__) || defined(__unix) || \
|
2023-01-11 14:50:10 +01:00
|
|
|
(defined(__APPLE__) && defined(__MACH__))
|
2014-11-21 09:52:23 +01:00
|
|
|
tv.tv_usec = (suseconds_t) usec % 1000000;
|
2014-08-16 12:45:52 +02:00
|
|
|
#else
|
2014-11-21 09:52:23 +01:00
|
|
|
tv.tv_usec = usec % 1000000;
|
2014-08-16 12:45:52 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
select(0, NULL, NULL, NULL, &tv);
|
2015-07-01 11:29:31 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read at most 'len' characters
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
|
2013-10-28 18:48:30 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-06-30 13:34:45 +02:00
|
|
|
int fd = ((mbedtls_net_context *) ctx)->fd;
|
2015-07-01 19:03:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = check_fd(fd, 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-07-01 19:03:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = (int) read(fd, buf, len);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
if (net_would_block(ctx) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
if (WSAGetLastError() == WSAECONNRESET) {
|
|
|
|
return MBEDTLS_ERR_NET_CONN_RESET;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (errno == EPIPE || errno == ECONNRESET) {
|
|
|
|
return MBEDTLS_ERR_NET_CONN_RESET;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (errno == EINTR) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_NET_RECV_FAILED;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-09-18 11:22:45 +02:00
|
|
|
/*
|
2014-10-01 15:01:39 +02:00
|
|
|
* Read at most 'len' characters, blocking for at most 'timeout' ms
|
2014-09-18 11:22:45 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf,
|
|
|
|
size_t len, uint32_t timeout)
|
2014-09-18 11:22:45 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-09-18 11:22:45 +02:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set read_fds;
|
2015-06-30 13:34:45 +02:00
|
|
|
int fd = ((mbedtls_net_context *) ctx)->fd;
|
2014-09-18 11:22:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = check_fd(fd, 1);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-02-24 19:49:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
FD_ZERO(&read_fds);
|
2023-03-06 23:51:39 +01:00
|
|
|
FD_SET((SOCKET) fd, &read_fds);
|
2014-09-18 11:22:45 +02:00
|
|
|
|
2014-10-01 15:01:39 +02:00
|
|
|
tv.tv_sec = timeout / 1000;
|
2023-01-11 14:50:10 +01:00
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
2014-09-18 11:22:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = select(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv);
|
2014-09-18 11:22:45 +02:00
|
|
|
|
|
|
|
/* Zero fds ready means we timed out */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_TIMEOUT;
|
|
|
|
}
|
2014-09-18 11:22:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
if (WSAGetLastError() == WSAEINTR) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2014-09-18 11:22:45 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (errno == EINTR) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2014-09-18 11:22:45 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_NET_RECV_FAILED;
|
2014-09-18 11:22:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This call will not block */
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_net_recv(ctx, buf, len);
|
2014-09-18 11:22:45 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Write at most 'len' characters
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-06-30 13:34:45 +02:00
|
|
|
int fd = ((mbedtls_net_context *) ctx)->fd;
|
2015-07-01 19:03:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = check_fd(fd, 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-07-01 19:03:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = (int) write(fd, buf, len);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
if (net_would_block(ctx) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
|
|
|
|
!defined(EFI32)
|
|
|
|
if (WSAGetLastError() == WSAECONNRESET) {
|
|
|
|
return MBEDTLS_ERR_NET_CONN_RESET;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (errno == EPIPE || errno == ECONNRESET) {
|
|
|
|
return MBEDTLS_ERR_NET_CONN_RESET;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (errno == EINTR) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_NET_SEND_FAILED;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:55:47 +02:00
|
|
|
/*
|
|
|
|
* Close the connection
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_net_close(mbedtls_net_context *ctx)
|
2019-08-23 10:55:47 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->fd == -1) {
|
2019-08-23 10:55:47 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-08-23 10:55:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
close(ctx->fd);
|
2019-08-23 10:55:47 +02:00
|
|
|
|
|
|
|
ctx->fd = -1;
|
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Gracefully close the connection
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_net_free(mbedtls_net_context *ctx)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->fd == -1) {
|
2015-06-30 13:34:45 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-06-30 13:34:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
shutdown(ctx->fd, 2);
|
|
|
|
close(ctx->fd);
|
2015-06-30 13:34:45 +02:00
|
|
|
|
|
|
|
ctx->fd = -1;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_NET_C */
|