blob: 2876f8fdd61d53abaa9ccd1393078df541f97f17 [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úti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010055#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
56 !defined(__APPLE__) && !defined(_WIN32)
57#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
58#endif
59
SimonBd5800b72016-04-26 07:43:27 +010060#if defined(MBEDTLS_PLATFORM_C)
61#include "mbedtls/platform.h"
62#else
63#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010064#endif
65
Andres AG788aa4a2016-09-14 14:32:09 +010066#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Rich Evans00ab4702015-02-06 13:43:58 +000068#include <string.h>
69
Paul Bakkerfa6a6202013-10-28 18:48:30 +010070#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
71 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Fabio Alessandrelliec4ce372018-04-03 19:40:11 +020073#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010074#undef _WIN32_WINNT
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010075/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010076#define _WIN32_WINNT 0x0501
Fabio Alessandrelliec4ce372018-04-03 19:40:11 +020077#endif
78
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010079#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010080
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010081#include <winsock2.h>
82#include <windows.h>
83
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010084#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000085#if defined(_WIN32_WCE)
86#pragma comment( lib, "ws2.lib" )
87#else
88#pragma comment( lib, "ws2_32.lib" )
89#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010090#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010092#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
93#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000094#define close(fd) closesocket(fd)
95
96static int wsa_init_done = 0;
97
Paul Bakkerdb20c102014-06-17 14:34:44 +020098#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100#include <sys/types.h>
101#include <sys/socket.h>
102#include <netinet/in.h>
103#include <arpa/inet.h>
104#include <sys/time.h>
105#include <unistd.h>
106#include <signal.h>
107#include <fcntl.h>
108#include <netdb.h>
109#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +0000110
Paul Bakkerdb20c102014-06-17 14:34:44 +0200111#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200113/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +0100114 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200115#if defined(_MSC_VER)
116#define MSVC_INT_CAST (int)
117#else
118#define MSVC_INT_CAST
119#endif
120
Paul Bakker5121ce52009-01-03 21:22:43 +0000121#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123#include <time.h>
124
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200125#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100128 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100130static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100132#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
133 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 WSADATA wsaData;
135
136 if( wsa_init_done == 0 )
137 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200138 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 wsa_init_done = 1;
142 }
143#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100144#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 signal( SIGPIPE, SIG_IGN );
146#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100147#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100148 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100149}
150
151/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200152 * Initialize a context
153 */
154void mbedtls_net_init( mbedtls_net_context *ctx )
155{
156 ctx->fd = -1;
157}
158
159/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100160 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100161 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000162int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
163 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100164{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100165 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100166 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100167
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100168 if( ( ret = net_prepare() ) != 0 )
169 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100170
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100171 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100172 memset( &hints, 0, sizeof( hints ) );
173 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
175 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100176
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200177 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100179
180 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100182 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
183 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200184 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100185 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200186 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100189 continue;
190 }
191
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200192 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100193 {
194 ret = 0;
195 break;
196 }
197
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200198 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100200 }
201
202 freeaddrinfo( addr_list );
203
204 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205}
206
207/*
208 * Create a listening socket on bind_ip:port
209 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200210int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100212 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100213 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100214
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100215 if( ( ret = net_prepare() ) != 0 )
216 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100217
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200218 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100219 memset( &hints, 0, sizeof( hints ) );
220 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
222 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100223 if( bind_ip == NULL )
224 hints.ai_flags = AI_PASSIVE;
225
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200226 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100228
229 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100231 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
232 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200233 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100234 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200235 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100238 continue;
239 }
240
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100241 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200242 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200243 (const char *) &n, sizeof( n ) ) != 0 )
244 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200245 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200247 continue;
248 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100249
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200250 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100251 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200252 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100254 continue;
255 }
256
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100257 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100259 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200260 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100261 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200262 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100264 continue;
265 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100266 }
267
Brian J Murray2adecba2016-11-06 04:45:15 -0800268 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100269 ret = 0;
270 break;
271 }
272
273 freeaddrinfo( addr_list );
274
275 return( ret );
276
Paul Bakker5121ce52009-01-03 21:22:43 +0000277}
278
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100279#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
280 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100281/*
282 * Check if the requested operation would be blocking on a non-blocking socket
283 * and thus 'failed' with a negative return value.
284 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200285static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100286{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200287 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100289}
Paul Bakker5121ce52009-01-03 21:22:43 +0000290#else
Paul Bakker80025412014-01-23 20:59:49 +0100291/*
292 * Check if the requested operation would be blocking on a non-blocking socket
293 * and thus 'failed' with a negative return value.
294 *
295 * Note: on a blocking socket this function always returns 0!
296 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200297static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100298{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100299 int err = errno;
Simon Butcherbb5e1c32018-06-08 11:14:43 +0100300
Paul Bakker80025412014-01-23 20:59:49 +0100301 /*
Jaeden Amero1529ede2019-05-29 09:38:29 +0100302 * Never return 'WOULD BLOCK' on a blocking socket
Paul Bakker80025412014-01-23 20:59:49 +0100303 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200304 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100305 {
306 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100307 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100308 }
Paul Bakker80025412014-01-23 20:59:49 +0100309
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100310 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 {
312#if defined EAGAIN
313 case EAGAIN:
314#endif
315#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
316 case EWOULDBLOCK:
317#endif
318 return( 1 );
319 }
320 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200322#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324/*
325 * Accept a connection from a remote client
326 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200327int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
328 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200329 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000330{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100331 int ret;
332 int type;
333
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100334 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
gufe443ca3b9e2020-08-17 07:14:16 +0200336#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
337 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t) || \
338 ( defined(__NetBSD__) && defined(socklen_t) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100340 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341#else
342 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100343 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344#endif
345
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100346 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200347 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
348 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100349 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100352 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100354 if( type == SOCK_STREAM )
355 {
356 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200357 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000358 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100359 }
360 else
361 {
362 /* UDP: wait for a message, but keep it in the queue */
363 char buf[1] = { 0 };
364
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700365 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100366 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200367
368#if defined(_WIN32)
369 if( ret == SOCKET_ERROR &&
370 WSAGetLastError() == WSAEMSGSIZE )
371 {
372 /* We know buf is too small, thanks, just peeking here */
373 ret = 0;
374 }
375#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100376 }
377
378 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200380 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100381 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 }
385
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200386 /* UDP: hijack the listening socket to communicate with the client,
387 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100388 if( type != SOCK_STREAM )
389 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200390 struct sockaddr_storage local_addr;
391 int one = 1;
392
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200393 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100395
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200396 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200397 bind_ctx->fd = -1; /* In case we exit early */
398
399 n = sizeof( struct sockaddr_storage );
400 if( getsockname( client_ctx->fd,
401 (struct sockaddr *) &local_addr, &n ) != 0 ||
402 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
403 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
404 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
405 (const char *) &one, sizeof( one ) ) != 0 )
406 {
407 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
408 }
409
410 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
411 {
412 return( MBEDTLS_ERR_NET_BIND_FAILED );
413 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100414 }
415
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100417 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100418 if( client_addr.ss_family == AF_INET )
419 {
420 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200421 *ip_len = sizeof( addr4->sin_addr.s_addr );
422
423 if( buf_size < *ip_len )
424 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
425
426 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100427 }
428 else
429 {
430 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200431 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
432
433 if( buf_size < *ip_len )
434 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
435
436 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100437 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100438 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440 return( 0 );
441}
442
443/*
444 * Set the socket blocking or non-blocking
445 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200446int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000447{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100448#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
449 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000450 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200451 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200453 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454#endif
455}
456
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200457int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000458{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100459#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
460 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000461 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200462 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200464 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465#endif
466}
467
468/*
469 * Portable usleep helper
470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000472{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200473#if defined(_WIN32)
474 Sleep( ( usec + 999 ) / 1000 );
475#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100477 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200478#if defined(__unix__) || defined(__unix) || \
479 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100480 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200481#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100482 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200483#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200485#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000486}
487
488/*
489 * Read at most 'len' characters
490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100492{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200493 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200494 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200495
496 if( fd < 0 )
497 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
498
499 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 if( ret < 0 )
502 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200503 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100504 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000505
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100506#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
507 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510#else
511 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100515 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516#endif
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 }
520
521 return( ret );
522}
523
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200524/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200525 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200528 uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200529{
530 int ret;
531 struct timeval tv;
532 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200533 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200534
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200535 if( fd < 0 )
536 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
537
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200538 FD_ZERO( &read_fds );
539 FD_SET( fd, &read_fds );
540
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200541 tv.tv_sec = timeout / 1000;
542 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200543
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200544 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200545
546 /* Zero fds ready means we timed out */
547 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100548 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200549
550 if( ret < 0 )
551 {
552#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
553 !defined(EFI32)
554 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100555 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200556#else
557 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100558 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200559#endif
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200562 }
563
564 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200566}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568/*
569 * Write at most 'len' characters
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000572{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200573 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200574 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200575
576 if( fd < 0 )
577 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
578
579 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 if( ret < 0 )
582 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200583 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100584 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100586#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
587 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590#else
591 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100595 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596#endif
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 }
600
601 return( ret );
602}
603
604/*
605 * Gracefully close the connection
606 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200607void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000608{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200609 if( ctx->fd == -1 )
610 return;
611
612 shutdown( ctx->fd, 2 );
613 close( ctx->fd );
614
615 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000616}
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618#endif /* MBEDTLS_NET_C */