blob: 574203d7c410010535a9db44110c12098d7d5494 [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útia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Nicholas Wilson2682edf2017-12-05 12:08:15 +000047/* Enable definition of getaddrinfo() even when compiling with -std=c99. Must
48 * be set before config.h, which pulls in glibc's features.h indirectly.
49 * Harmless on other platforms. */
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070050#ifndef _POSIX_C_SOURCE
Nicholas Wilson2682edf2017-12-05 12:08:15 +000051#define _POSIX_C_SOURCE 200112L
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070052#endif
Nicholas Wilson2682edf2017-12-05 12:08:15 +000053
gufe4419735b62020-08-13 11:03:54 +020054#if defined(__NetBSD__)
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070055#ifndef _XOPEN_SOURCE
gufe4419735b62020-08-13 11:03:54 +020056#define _XOPEN_SOURCE 600 /* sockaddr_storage */
57#endif
Flavio Ceolin9911e4a2020-05-27 22:41:19 -070058#endif
gufe4419735b62020-08-13 11:03:54 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020062#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010068#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040069 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
70 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010071#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
72#endif
73
SimonBd5800b72016-04-26 07:43:27 +010074#if defined(MBEDTLS_PLATFORM_C)
75#include "mbedtls/platform.h"
76#else
77#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010078#endif
79
Andres AG788aa4a2016-09-14 14:32:09 +010080#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Rich Evans00ab4702015-02-06 13:43:58 +000082#include <string.h>
83
Paul Bakkerfa6a6202013-10-28 18:48:30 +010084#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
85 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010087#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000088
Fabio Alessandrellidf608562018-04-03 19:40:11 +020089#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010090#undef _WIN32_WINNT
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010091/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010092#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020093#endif
94
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010095#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010096
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010097#include <winsock2.h>
98#include <windows.h>
99
Paul Bakkerf0fc2a22013-12-30 15:42:43 +0100100#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101#if defined(_WIN32_WCE)
102#pragma comment( lib, "ws2.lib" )
103#else
104#pragma comment( lib, "ws2_32.lib" )
105#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +0100106#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100108#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
109#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110#define close(fd) closesocket(fd)
111
112static int wsa_init_done = 0;
113
Paul Bakkerdb20c102014-06-17 14:34:44 +0200114#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116#include <sys/types.h>
117#include <sys/socket.h>
118#include <netinet/in.h>
119#include <arpa/inet.h>
120#include <sys/time.h>
121#include <unistd.h>
122#include <signal.h>
123#include <fcntl.h>
124#include <netdb.h>
125#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000126
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100127#define IS_EINTR( ret ) ( ( ret ) == EINTR )
Hanno Beckeref527962018-03-15 15:49:24 +0000128
Paul Bakkerdb20c102014-06-17 14:34:44 +0200129#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200131/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100132 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200133#if defined(_MSC_VER)
134#define MSVC_INT_CAST (int)
135#else
136#define MSVC_INT_CAST
137#endif
138
Paul Bakker5121ce52009-01-03 21:22:43 +0000139#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141#include <time.h>
142
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200143#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000144
Paul Bakker5121ce52009-01-03 21:22:43 +0000145/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100146 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100148static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100150#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
151 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 WSADATA wsaData;
153
154 if( wsa_init_done == 0 )
155 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200156 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 wsa_init_done = 1;
160 }
161#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100162#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 signal( SIGPIPE, SIG_IGN );
164#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100165#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100166 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100167}
168
169/*
Gilles Peskine9065d782021-06-20 23:08:19 +0200170 * Return 0 if the file descriptor is valid, an error otherwise.
171 * If for_select != 0, check whether the file descriptor is within the range
172 * allowed for fd_set used for the FD_xxx macros and the select() function.
173 */
174static int check_fd( int fd, int for_select )
175{
176 if( fd < 0 )
177 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
178
179 /* A limitation of select() is that it only works with file descriptors
180 * that are strictly less than FD_SETSIZE. This is a limitation of the
181 * fd_set type. Error out early, because attempting to call FD_SET on a
182 * large file descriptor is a buffer overflow on typical platforms. */
183 if( for_select && fd >= FD_SETSIZE )
184 return( MBEDTLS_ERR_NET_POLL_FAILED );
185
186 return( 0 );
187}
188
189/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200190 * Initialize a context
191 */
192void mbedtls_net_init( mbedtls_net_context *ctx )
193{
194 ctx->fd = -1;
195}
196
197/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100198 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100199 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000200int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
201 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100202{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100203 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100204 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100205
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100206 if( ( ret = net_prepare() ) != 0 )
207 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100208
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100209 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100210 memset( &hints, 0, sizeof( hints ) );
211 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
213 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100214
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200215 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100217
218 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100220 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
221 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200222 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100223 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200224 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100227 continue;
228 }
229
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200230 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100231 {
232 ret = 0;
233 break;
234 }
235
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200236 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100238 }
239
240 freeaddrinfo( addr_list );
241
242 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243}
244
245/*
246 * Create a listening socket on bind_ip:port
247 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200248int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100250 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100251 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100252
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100253 if( ( ret = net_prepare() ) != 0 )
254 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100255
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200256 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100257 memset( &hints, 0, sizeof( hints ) );
258 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
260 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100261 if( bind_ip == NULL )
262 hints.ai_flags = AI_PASSIVE;
263
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200264 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100266
267 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
270 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200271 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100272 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200273 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100276 continue;
277 }
278
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100279 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200280 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200281 (const char *) &n, sizeof( n ) ) != 0 )
282 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200283 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200285 continue;
286 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100287
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200288 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100289 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200290 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100292 continue;
293 }
294
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100295 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100297 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200298 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100299 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200300 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100302 continue;
303 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100304 }
305
Brian J Murray2adecba2016-11-06 04:45:15 -0800306 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100307 ret = 0;
308 break;
309 }
310
311 freeaddrinfo( addr_list );
312
313 return( ret );
314
Paul Bakker5121ce52009-01-03 21:22:43 +0000315}
316
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100317#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
318 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100319/*
320 * Check if the requested operation would be blocking on a non-blocking socket
321 * and thus 'failed' with a negative return value.
322 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200323static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100324{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200325 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100327}
Paul Bakker5121ce52009-01-03 21:22:43 +0000328#else
Paul Bakker80025412014-01-23 20:59:49 +0100329/*
330 * Check if the requested operation would be blocking on a non-blocking socket
331 * and thus 'failed' with a negative return value.
332 *
333 * Note: on a blocking socket this function always returns 0!
334 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200335static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100336{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100337 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100338
Paul Bakker80025412014-01-23 20:59:49 +0100339 /*
Jaeden Amero88a6e892019-05-29 09:38:29 +0100340 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100341 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200342 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100343 {
344 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100345 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100346 }
Paul Bakker80025412014-01-23 20:59:49 +0100347
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100348 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 {
350#if defined EAGAIN
351 case EAGAIN:
352#endif
353#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
354 case EWOULDBLOCK:
355#endif
356 return( 1 );
357 }
358 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200360#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362/*
363 * Accept a connection from a remote client
364 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200365int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
366 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200367 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000368{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100369 int ret;
370 int type;
371
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100372 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
gufe4419735b62020-08-13 11:03:54 +0200374#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
375 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
376 ( defined(__NetBSD__) && defined(socklen_t) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100378 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379#else
380 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100381 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382#endif
383
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100384 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200385 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
386 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100387 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100390 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100392 if( type == SOCK_STREAM )
393 {
394 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200395 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000396 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100397 }
398 else
399 {
400 /* UDP: wait for a message, but keep it in the queue */
401 char buf[1] = { 0 };
402
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700403 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100404 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200405
406#if defined(_WIN32)
407 if( ret == SOCKET_ERROR &&
408 WSAGetLastError() == WSAEMSGSIZE )
409 {
410 /* We know buf is too small, thanks, just peeking here */
411 ret = 0;
412 }
413#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100414 }
415
416 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200418 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100419 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 }
423
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200424 /* UDP: hijack the listening socket to communicate with the client,
425 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100426 if( type != SOCK_STREAM )
427 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200428 struct sockaddr_storage local_addr;
429 int one = 1;
430
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200431 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100433
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200434 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200435 bind_ctx->fd = -1; /* In case we exit early */
436
437 n = sizeof( struct sockaddr_storage );
438 if( getsockname( client_ctx->fd,
439 (struct sockaddr *) &local_addr, &n ) != 0 ||
440 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
441 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
442 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
443 (const char *) &one, sizeof( one ) ) != 0 )
444 {
445 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
446 }
447
448 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
449 {
450 return( MBEDTLS_ERR_NET_BIND_FAILED );
451 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100452 }
453
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100455 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100456 if( client_addr.ss_family == AF_INET )
457 {
458 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200459 *ip_len = sizeof( addr4->sin_addr.s_addr );
460
461 if( buf_size < *ip_len )
462 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
463
464 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100465 }
466 else
467 {
468 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200469 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
470
471 if( buf_size < *ip_len )
472 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
473
474 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100475 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100476 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478 return( 0 );
479}
480
481/*
482 * Set the socket blocking or non-blocking
483 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200484int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000485{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100486#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
487 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000488 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200489 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200491 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492#endif
493}
494
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200495int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100497#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
498 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000499 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200500 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200502 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503#endif
504}
505
506/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100507 * Check if data is available on the socket
508 */
509
510int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
511{
512 int ret;
513 struct timeval tv;
514
515 fd_set read_fds;
516 fd_set write_fds;
517
518 int fd = ctx->fd;
519
Gilles Peskine9065d782021-06-20 23:08:19 +0200520 ret = check_fd( fd, 1 );
521 if( ret != 0 )
522 return( ret );
Gilles Peskinef6042402021-02-24 19:49:44 +0100523
Gilles Peskineec4733b2018-04-05 14:55:47 +0200524#if defined(__has_feature)
525#if __has_feature(memory_sanitizer)
526 /* Ensure that memory sanitizers consider read_fds and write_fds as
527 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
528 * is implemented in assembly. */
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100529 memset( &read_fds, 0, sizeof( read_fds ) );
530 memset( &write_fds, 0, sizeof( write_fds ) );
Gilles Peskineec4733b2018-04-05 14:55:47 +0200531#endif
532#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100533
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100534 FD_ZERO( &read_fds );
535 if( rw & MBEDTLS_NET_POLL_READ )
536 {
537 rw &= ~MBEDTLS_NET_POLL_READ;
538 FD_SET( fd, &read_fds );
539 }
540
541 FD_ZERO( &write_fds );
542 if( rw & MBEDTLS_NET_POLL_WRITE )
543 {
544 rw &= ~MBEDTLS_NET_POLL_WRITE;
545 FD_SET( fd, &write_fds );
546 }
547
548 if( rw != 0 )
549 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
550
551 tv.tv_sec = timeout / 1000;
552 tv.tv_usec = ( timeout % 1000 ) * 1000;
553
Hanno Becker9ac64032018-03-15 12:19:31 +0000554 do
555 {
556 ret = select( fd + 1, &read_fds, &write_fds, NULL,
557 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000558 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100559 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100560
561 if( ret < 0 )
562 return( MBEDTLS_ERR_NET_POLL_FAILED );
563
564 ret = 0;
565 if( FD_ISSET( fd, &read_fds ) )
566 ret |= MBEDTLS_NET_POLL_READ;
567 if( FD_ISSET( fd, &write_fds ) )
568 ret |= MBEDTLS_NET_POLL_WRITE;
569
570 return( ret );
571}
572
573/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 * Portable usleep helper
575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000577{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200578#if defined(_WIN32)
579 Sleep( ( usec + 999 ) / 1000 );
580#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100582 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200583#if defined(__unix__) || defined(__unix) || \
584 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100585 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200586#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100587 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200590#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000591}
592
593/*
594 * Read at most 'len' characters
595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100597{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200598 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200599 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200600
Gilles Peskine9065d782021-06-20 23:08:19 +0200601 ret = check_fd( fd, 0 );
602 if( ret != 0 )
603 return( ret );
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200604
605 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 if( ret < 0 )
608 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200609 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100610 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100612#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
613 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616#else
617 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100621 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622#endif
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 }
626
627 return( ret );
628}
629
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200630/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200631 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200632 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100633int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
634 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200635{
636 int ret;
637 struct timeval tv;
638 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200639 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200640
Gilles Peskine9065d782021-06-20 23:08:19 +0200641 ret = check_fd( fd, 1 );
642 if( ret != 0 )
643 return( ret );
Gilles Peskinef6042402021-02-24 19:49:44 +0100644
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200645 FD_ZERO( &read_fds );
646 FD_SET( fd, &read_fds );
647
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200648 tv.tv_sec = timeout / 1000;
649 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200650
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200651 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200652
653 /* Zero fds ready means we timed out */
654 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100655 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200656
657 if( ret < 0 )
658 {
659#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
660 !defined(EFI32)
661 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100662 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200663#else
664 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100665 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200666#endif
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200669 }
670
671 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200673}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200674
Paul Bakker5121ce52009-01-03 21:22:43 +0000675/*
676 * Write at most 'len' characters
677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000679{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200680 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200681 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200682
Gilles Peskine9065d782021-06-20 23:08:19 +0200683 ret = check_fd( fd, 0 );
684 if( ret != 0 )
685 return( ret );
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200686
687 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
689 if( ret < 0 )
690 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200691 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100692 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000693
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100694#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
695 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000698#else
699 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100703 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704#endif
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 }
708
709 return( ret );
710}
711
712/*
713 * Gracefully close the connection
714 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200715void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000716{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200717 if( ctx->fd == -1 )
718 return;
719
720 shutdown( ctx->fd, 2 );
721 close( ctx->fd );
722
723 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000724}
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_NET_C */