blob: 202da01714a0a3f6b506150d9fcdc2ce928e920d [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010030#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Matthias Weisser3f21a352016-08-18 07:55:05 +020031 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010032#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
33#endif
34
SimonBd5800b72016-04-26 07:43:27 +010035#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#else
38#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010039#endif
40
Andres AG788aa4a2016-09-14 14:32:09 +010041#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Paul Bakkerfa6a6202013-10-28 18:48:30 +010045#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
46 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010048#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000049
Fabio Alessandrellidf608562018-04-03 19:40:11 +020050#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0501)
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010051#undef _WIN32_WINNT
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010052/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010053#define _WIN32_WINNT 0x0501
Fabio Alessandrellidf608562018-04-03 19:40:11 +020054#endif
55
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010056#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010057
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010058#include <winsock2.h>
59#include <windows.h>
60
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010061#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000062#if defined(_WIN32_WCE)
63#pragma comment( lib, "ws2.lib" )
64#else
65#pragma comment( lib, "ws2_32.lib" )
66#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010067#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010069#define read(fd,buf,len) recv( fd, (char*)( buf ), (int)( len ), 0 )
70#define write(fd,buf,len) send( fd, (char*)( buf ), (int)( len ), 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000071#define close(fd) closesocket(fd)
72
73static int wsa_init_done = 0;
74
Paul Bakkerdb20c102014-06-17 14:34:44 +020075#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77#include <sys/types.h>
78#include <sys/socket.h>
79#include <netinet/in.h>
80#include <arpa/inet.h>
81#include <sys/time.h>
82#include <unistd.h>
83#include <signal.h>
84#include <fcntl.h>
85#include <netdb.h>
86#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000087
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010088#define IS_EINTR( ret ) ( ( ret ) == EINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000089
Paul Bakkerdb20c102014-06-17 14:34:44 +020090#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020092/* Some MS functions want int and MSVC warns if we pass size_t,
Andres Amaya Garciabd9d42c2017-07-12 14:04:40 +010093 * but the standard functions use socklen_t, so cast only for MSVC */
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020094#if defined(_MSC_VER)
95#define MSVC_INT_CAST (int)
96#else
97#define MSVC_INT_CAST
98#endif
99
Paul Bakker5121ce52009-01-03 21:22:43 +0000100#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200101
Paul Bakker5121ce52009-01-03 21:22:43 +0000102#include <time.h>
103
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +0200104#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +0000105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100107 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100109static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100111#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
112 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 WSADATA wsaData;
114
115 if( wsa_init_done == 0 )
116 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200117 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120 wsa_init_done = 1;
121 }
122#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100123#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 signal( SIGPIPE, SIG_IGN );
125#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100126#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100127 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100128}
129
130/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200131 * Initialize a context
132 */
133void mbedtls_net_init( mbedtls_net_context *ctx )
134{
135 ctx->fd = -1;
136}
137
138/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100139 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100140 */
Simon Butcher532c94d2016-11-10 17:30:18 +0000141int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host,
142 const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100143{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100144 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100145 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100146
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100147 if( ( ret = net_prepare() ) != 0 )
148 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100149
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100150 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100151 memset( &hints, 0, sizeof( hints ) );
152 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
154 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100155
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200156 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100158
159 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100161 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
162 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200163 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100164 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200165 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100168 continue;
169 }
170
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200171 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100172 {
173 ret = 0;
174 break;
175 }
176
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200177 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100179 }
180
181 freeaddrinfo( addr_list );
182
183 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
185
186/*
187 * Create a listening socket on bind_ip:port
188 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200189int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100191 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100192 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100193
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100194 if( ( ret = net_prepare() ) != 0 )
195 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100196
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200197 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100198 memset( &hints, 0, sizeof( hints ) );
199 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
201 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100202 if( bind_ip == NULL )
203 hints.ai_flags = AI_PASSIVE;
204
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200205 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100207
208 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100210 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
211 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200212 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100213 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200214 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100217 continue;
218 }
219
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100220 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200221 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200222 (const char *) &n, sizeof( n ) ) != 0 )
223 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200224 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200226 continue;
227 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100228
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200229 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100230 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200231 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100233 continue;
234 }
235
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100236 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100238 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200239 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100240 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200241 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 ret = MBEDTLS_ERR_NET_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100243 continue;
244 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100245 }
246
Brian J Murray2adecba2016-11-06 04:45:15 -0800247 /* Bind was successful */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100248 ret = 0;
249 break;
250 }
251
252 freeaddrinfo( addr_list );
253
254 return( ret );
255
Paul Bakker5121ce52009-01-03 21:22:43 +0000256}
257
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100258#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
259 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100260/*
261 * Check if the requested operation would be blocking on a non-blocking socket
262 * and thus 'failed' with a negative return value.
263 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200264static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100265{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200266 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100268}
Paul Bakker5121ce52009-01-03 21:22:43 +0000269#else
Paul Bakker80025412014-01-23 20:59:49 +0100270/*
271 * Check if the requested operation would be blocking on a non-blocking socket
272 * and thus 'failed' with a negative return value.
273 *
274 * Note: on a blocking socket this function always returns 0!
275 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200276static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100277{
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100278 int err = errno;
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100279
Paul Bakker80025412014-01-23 20:59:49 +0100280 /*
281 * Never return 'WOULD BLOCK' on a non-blocking socket
282 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200283 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100284 {
285 errno = err;
Paul Bakker80025412014-01-23 20:59:49 +0100286 return( 0 );
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100287 }
Paul Bakker80025412014-01-23 20:59:49 +0100288
Hanno Beckera6ed9c52017-05-04 13:39:22 +0100289 switch( errno = err )
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 {
291#if defined EAGAIN
292 case EAGAIN:
293#endif
294#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
295 case EWOULDBLOCK:
296#endif
297 return( 1 );
298 }
299 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200301#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303/*
304 * Accept a connection from a remote client
305 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200306int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
307 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200308 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100310 int ret;
311 int type;
312
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100313 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Paul Bakker394c56f2011-12-20 12:19:03 +0000315#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
Manuel Pégourié-Gonnard04317352015-10-05 12:16:06 +0100316 defined(_SOCKLEN_T_DECLARED) || defined(__DEFINED_socklen_t)
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100318 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319#else
320 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100321 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322#endif
323
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100324 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200325 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
326 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100327 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100330 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100332 if( type == SOCK_STREAM )
333 {
334 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200335 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Simon Butcher532c94d2016-11-10 17:30:18 +0000336 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100337 }
338 else
339 {
340 /* UDP: wait for a message, but keep it in the queue */
341 char buf[1] = { 0 };
342
Embedthis Softwarea25cab82015-09-09 08:49:48 -0700343 ret = (int) recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100344 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200345
346#if defined(_WIN32)
347 if( ret == SOCKET_ERROR &&
348 WSAGetLastError() == WSAEMSGSIZE )
349 {
350 /* We know buf is too small, thanks, just peeking here */
351 ret = 0;
352 }
353#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100354 }
355
356 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200358 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100359 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 }
363
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200364 /* UDP: hijack the listening socket to communicate with the client,
365 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100366 if( type != SOCK_STREAM )
367 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200368 struct sockaddr_storage local_addr;
369 int one = 1;
370
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200371 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100373
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200374 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200375 bind_ctx->fd = -1; /* In case we exit early */
376
377 n = sizeof( struct sockaddr_storage );
378 if( getsockname( client_ctx->fd,
379 (struct sockaddr *) &local_addr, &n ) != 0 ||
380 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
381 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
382 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
383 (const char *) &one, sizeof( one ) ) != 0 )
384 {
385 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
386 }
387
388 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
389 {
390 return( MBEDTLS_ERR_NET_BIND_FAILED );
391 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100392 }
393
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100395 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100396 if( client_addr.ss_family == AF_INET )
397 {
398 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200399 *ip_len = sizeof( addr4->sin_addr.s_addr );
400
401 if( buf_size < *ip_len )
402 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
403
404 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100405 }
406 else
407 {
408 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200409 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
410
411 if( buf_size < *ip_len )
412 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
413
414 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100415 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100416 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 return( 0 );
419}
420
421/*
422 * Set the socket blocking or non-blocking
423 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200424int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000425{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100426#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
427 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000428 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200429 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200431 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432#endif
433}
434
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200435int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000436{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100437#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
438 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000439 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200440 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200442 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443#endif
444}
445
446/*
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100447 * Check if data is available on the socket
448 */
449
450int mbedtls_net_poll( mbedtls_net_context *ctx, uint32_t rw, uint32_t timeout )
451{
452 int ret;
453 struct timeval tv;
454
455 fd_set read_fds;
456 fd_set write_fds;
457
458 int fd = ctx->fd;
459
460 if( fd < 0 )
461 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
462
Gilles Peskineec4733b2018-04-05 14:55:47 +0200463#if defined(__has_feature)
464#if __has_feature(memory_sanitizer)
465 /* Ensure that memory sanitizers consider read_fds and write_fds as
466 * initialized even on platforms such as Glibc/x86_64 where FD_ZERO
467 * is implemented in assembly. */
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100468 memset( &read_fds, 0, sizeof( read_fds ) );
469 memset( &write_fds, 0, sizeof( write_fds ) );
Gilles Peskineec4733b2018-04-05 14:55:47 +0200470#endif
471#endif
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100472
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100473 FD_ZERO( &read_fds );
474 if( rw & MBEDTLS_NET_POLL_READ )
475 {
476 rw &= ~MBEDTLS_NET_POLL_READ;
477 FD_SET( fd, &read_fds );
478 }
479
480 FD_ZERO( &write_fds );
481 if( rw & MBEDTLS_NET_POLL_WRITE )
482 {
483 rw &= ~MBEDTLS_NET_POLL_WRITE;
484 FD_SET( fd, &write_fds );
485 }
486
487 if( rw != 0 )
488 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
489
490 tv.tv_sec = timeout / 1000;
491 tv.tv_usec = ( timeout % 1000 ) * 1000;
492
Hanno Becker9ac64032018-03-15 12:19:31 +0000493 do
494 {
495 ret = select( fd + 1, &read_fds, &write_fds, NULL,
496 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000497 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100498 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100499
500 if( ret < 0 )
501 return( MBEDTLS_ERR_NET_POLL_FAILED );
502
503 ret = 0;
504 if( FD_ISSET( fd, &read_fds ) )
505 ret |= MBEDTLS_NET_POLL_READ;
506 if( FD_ISSET( fd, &write_fds ) )
507 ret |= MBEDTLS_NET_POLL_WRITE;
508
509 return( ret );
510}
511
512/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 * Portable usleep helper
514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200517#if defined(_WIN32)
518 Sleep( ( usec + 999 ) / 1000 );
519#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100521 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200522#if defined(__unix__) || defined(__unix) || \
523 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100524 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200525#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100526 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200527#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000530}
531
532/*
533 * Read at most 'len' characters
534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100536{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200537 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200538 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200539
540 if( fd < 0 )
541 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
542
543 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 if( ret < 0 )
546 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200547 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100548 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100550#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
551 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554#else
555 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100559 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560#endif
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 }
564
565 return( ret );
566}
567
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200568/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200569 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200570 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100571int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
572 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200573{
574 int ret;
575 struct timeval tv;
576 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200577 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200578
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200579 if( fd < 0 )
580 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
581
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200582 FD_ZERO( &read_fds );
583 FD_SET( fd, &read_fds );
584
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200585 tv.tv_sec = timeout / 1000;
586 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200587
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200588 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200589
590 /* Zero fds ready means we timed out */
591 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100592 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200593
594 if( ret < 0 )
595 {
596#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
597 !defined(EFI32)
598 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100599 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200600#else
601 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100602 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200603#endif
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200606 }
607
608 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200610}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200611
Paul Bakker5121ce52009-01-03 21:22:43 +0000612/*
613 * Write at most 'len' characters
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000616{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200617 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200618 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200619
620 if( fd < 0 )
621 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
622
623 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
625 if( ret < 0 )
626 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200627 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100628 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100630#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
631 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634#else
635 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637
638 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100639 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640#endif
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 }
644
645 return( ret );
646}
647
648/*
649 * Gracefully close the connection
650 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200651void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000652{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200653 if( ctx->fd == -1 )
654 return;
655
656 shutdown( ctx->fd, 2 );
657 close( ctx->fd );
658
659 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660}
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_NET_C */