blob: e68b3ef05fa8794fb4881c31e86ea4e09da62b4a [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) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040031 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
32 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010033#error "This module only works on Unix and Windows, see MBEDTLS_NET_C in config.h"
34#endif
35
SimonBd5800b72016-04-26 07:43:27 +010036#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdlib.h>
SimonBd5800b72016-04-26 07:43:27 +010040#endif
41
Andres AG788aa4a2016-09-14 14:32:09 +010042#include "mbedtls/net_sockets.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <string.h>
45
Paul Bakkerfa6a6202013-10-28 18:48:30 +010046#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
47 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Gilles Peskineea7dbbe2018-03-15 23:25:21 +010049#define IS_EINTR( ret ) ( ( ret ) == WSAEINTR )
Hanno Beckeref527962018-03-15 15:49:24 +000050
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010051#ifdef _WIN32_WINNT
52#undef _WIN32_WINNT
53#endif
54/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010055#define _WIN32_WINNT 0x0501
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
Hanno Beckerf4e5b7e2018-04-03 16:28:09 +0100463 /* Ensure that memory sanitizers consider
464 * read_fds and write_fds as initialized even
465 * if FD_ZERO is implemented in assembly. */
466 memset( &read_fds, 0, sizeof( read_fds ) );
467 memset( &write_fds, 0, sizeof( write_fds ) );
468
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100469 FD_ZERO( &read_fds );
470 if( rw & MBEDTLS_NET_POLL_READ )
471 {
472 rw &= ~MBEDTLS_NET_POLL_READ;
473 FD_SET( fd, &read_fds );
474 }
475
476 FD_ZERO( &write_fds );
477 if( rw & MBEDTLS_NET_POLL_WRITE )
478 {
479 rw &= ~MBEDTLS_NET_POLL_WRITE;
480 FD_SET( fd, &write_fds );
481 }
482
483 if( rw != 0 )
484 return( MBEDTLS_ERR_NET_BAD_INPUT_DATA );
485
486 tv.tv_sec = timeout / 1000;
487 tv.tv_usec = ( timeout % 1000 ) * 1000;
488
Hanno Becker9ac64032018-03-15 12:19:31 +0000489 do
490 {
491 ret = select( fd + 1, &read_fds, &write_fds, NULL,
492 timeout == (uint32_t) -1 ? NULL : &tv );
Hanno Becker80e06d72018-03-15 14:41:55 +0000493 }
Gilles Peskineea7dbbe2018-03-15 23:25:21 +0100494 while( IS_EINTR( ret ) );
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100495
496 if( ret < 0 )
497 return( MBEDTLS_ERR_NET_POLL_FAILED );
498
499 ret = 0;
500 if( FD_ISSET( fd, &read_fds ) )
501 ret |= MBEDTLS_NET_POLL_READ;
502 if( FD_ISSET( fd, &write_fds ) )
503 ret |= MBEDTLS_NET_POLL_WRITE;
504
505 return( ret );
506}
507
508/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 * Portable usleep helper
510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200513#if defined(_WIN32)
514 Sleep( ( usec + 999 ) / 1000 );
515#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100517 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200518#if defined(__unix__) || defined(__unix) || \
519 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100520 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200521#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100522 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200523#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000526}
527
528/*
529 * Read at most 'len' characters
530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100532{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200533 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200534 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200535
536 if( fd < 0 )
537 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
538
539 ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 if( ret < 0 )
542 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200543 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100544 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100546#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
547 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550#else
551 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100555 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556#endif
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 }
560
561 return( ret );
562}
563
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200564/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200565 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200566 */
Hanno Beckere09ca3d2017-05-22 15:06:06 +0100567int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf,
568 size_t len, uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200569{
570 int ret;
571 struct timeval tv;
572 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200573 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200574
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200575 if( fd < 0 )
576 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
577
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200578 FD_ZERO( &read_fds );
579 FD_SET( fd, &read_fds );
580
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200581 tv.tv_sec = timeout / 1000;
582 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200583
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200584 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200585
586 /* Zero fds ready means we timed out */
587 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100588 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200589
590 if( ret < 0 )
591 {
592#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
593 !defined(EFI32)
594 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100595 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200596#else
597 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100598 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200599#endif
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200602 }
603
604 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200606}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200607
Paul Bakker5121ce52009-01-03 21:22:43 +0000608/*
609 * Write at most 'len' characters
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000612{
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200613 int ret;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200614 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9bd0afd2015-07-01 19:03:27 +0200615
616 if( fd < 0 )
617 return( MBEDTLS_ERR_NET_INVALID_CONTEXT );
618
619 ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 if( ret < 0 )
622 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200623 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100624 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100626#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
627 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630#else
631 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100635 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636#endif
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
640
641 return( ret );
642}
643
644/*
645 * Gracefully close the connection
646 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200647void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000648{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200649 if( ctx->fd == -1 )
650 return;
651
652 shutdown( ctx->fd, 2 );
653 close( ctx->fd );
654
655 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656}
657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_NET_C */