blob: 2012aa6be2fbf1dbc3791d25e6d3c547b73dc3a6 [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é-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_NET_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/net.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
36 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Manuel Pégourié-Gonnard3b6269a2014-03-21 10:31:12 +010038#ifdef _WIN32_WINNT
39#undef _WIN32_WINNT
40#endif
41/* Enables getaddrinfo() & Co */
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010042#define _WIN32_WINNT 0x0501
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010043#include <ws2tcpip.h>
Manuel Pégourié-Gonnard6a398d42013-12-17 16:10:58 +010044
Manuel Pégourié-Gonnard13211352013-12-17 17:38:55 +010045#include <winsock2.h>
46#include <windows.h>
47
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010048#if defined(_MSC_VER)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#if defined(_WIN32_WCE)
50#pragma comment( lib, "ws2.lib" )
51#else
52#pragma comment( lib, "ws2_32.lib" )
53#endif
Paul Bakkerf0fc2a22013-12-30 15:42:43 +010054#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakkerf4f69682011-04-24 16:08:12 +000056#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
57#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
Paul Bakker5121ce52009-01-03 21:22:43 +000058#define close(fd) closesocket(fd)
59
60static int wsa_init_done = 0;
61
Paul Bakkerdb20c102014-06-17 14:34:44 +020062#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64#include <sys/types.h>
65#include <sys/socket.h>
66#include <netinet/in.h>
67#include <arpa/inet.h>
68#include <sys/time.h>
69#include <unistd.h>
70#include <signal.h>
71#include <fcntl.h>
72#include <netdb.h>
73#include <errno.h>
Paul Bakkerb3bb6c02009-07-27 21:09:47 +000074
Paul Bakkerdb20c102014-06-17 14:34:44 +020075#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +020077/* Some MS functions want int and MSVC warns if we pass size_t,
78 * but the standard fucntions use socklen_t, so cast only for MSVC */
79#if defined(_MSC_VER)
80#define MSVC_INT_CAST (int)
81#else
82#define MSVC_INT_CAST
83#endif
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085#include <stdlib.h>
86#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020087
Paul Bakker5121ce52009-01-03 21:22:43 +000088#include <time.h>
89
Paul Bakkerfa6a6202013-10-28 18:48:30 +010090#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000091#include <basetsd.h>
92typedef UINT32 uint32_t;
93#else
Manuel Pégourié-Gonnard93866642015-06-22 19:21:23 +020094#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000095#endif
96
Paul Bakker5121ce52009-01-03 21:22:43 +000097/*
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +010098 * Prepare for using the sockets interface
Paul Bakker5121ce52009-01-03 21:22:43 +000099 */
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100100static int net_prepare( void )
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100102#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
103 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 WSADATA wsaData;
105
106 if( wsa_init_done == 0 )
107 {
Peter Vaskovic7015de72014-05-15 02:54:37 +0200108 if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111 wsa_init_done = 1;
112 }
113#else
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100114#if !defined(EFIX64) && !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 signal( SIGPIPE, SIG_IGN );
116#endif
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100117#endif
Manuel Pégourié-Gonnardee5db1d2013-12-17 16:46:19 +0100118 return( 0 );
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100119}
120
121/*
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200122 * Initialize a context
123 */
124void mbedtls_net_init( mbedtls_net_context *ctx )
125{
126 ctx->fd = -1;
127}
128
129/*
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100130 * Initiate a TCP connection with host:port and the given protocol
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100131 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200132int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto )
Manuel Pégourié-Gonnard2e5c3162013-12-13 11:55:32 +0100133{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100134 int ret;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100135 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100136
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100137 if( ( ret = net_prepare() ) != 0 )
138 return( ret );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100139
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100140 /* Do name resolution with both IPv6 and IPv4 */
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100141 memset( &hints, 0, sizeof( hints ) );
142 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
144 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100145
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200146 if( getaddrinfo( host, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100148
149 /* Try the sockaddrs until a connection succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100151 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
152 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200153 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100154 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200155 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100158 continue;
159 }
160
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200161 if( connect( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) == 0 )
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100162 {
163 ret = 0;
164 break;
165 }
166
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200167 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 ret = MBEDTLS_ERR_NET_CONNECT_FAILED;
Manuel Pégourié-Gonnard10934de2013-12-13 12:54:09 +0100169 }
170
171 freeaddrinfo( addr_list );
172
173 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
175
176/*
177 * Create a listening socket on bind_ip:port
178 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200179int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto )
Paul Bakker5121ce52009-01-03 21:22:43 +0000180{
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100181 int n, ret;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100182 struct addrinfo hints, *addr_list, *cur;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100183
Manuel Pégourié-Gonnard173402b2013-12-17 15:57:05 +0100184 if( ( ret = net_prepare() ) != 0 )
185 return( ret );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100186
Manuel Pégourié-Gonnarddb2468d2015-06-30 17:19:48 +0200187 /* Bind to IPv6 and/or IPv4, but only in the desired protocol */
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100188 memset( &hints, 0, sizeof( hints ) );
189 hints.ai_family = AF_UNSPEC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
191 hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100192 if( bind_ip == NULL )
193 hints.ai_flags = AI_PASSIVE;
194
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200195 if( getaddrinfo( bind_ip, port, &hints, &addr_list ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_NET_UNKNOWN_HOST );
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100197
198 /* Try the sockaddrs until a binding succeeds */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 ret = MBEDTLS_ERR_NET_UNKNOWN_HOST;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100200 for( cur = addr_list; cur != NULL; cur = cur->ai_next )
201 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200202 ctx->fd = (int) socket( cur->ai_family, cur->ai_socktype,
Paul Bakker00f5c522013-12-31 10:45:16 +0100203 cur->ai_protocol );
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200204 if( ctx->fd < 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100207 continue;
208 }
209
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100210 n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200211 if( setsockopt( ctx->fd, SOL_SOCKET, SO_REUSEADDR,
Paul Bakker874bd642014-04-17 12:43:05 +0200212 (const char *) &n, sizeof( n ) ) != 0 )
213 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200214 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 ret = MBEDTLS_ERR_NET_SOCKET_FAILED;
Paul Bakker874bd642014-04-17 12:43:05 +0200216 continue;
217 }
Manuel Pégourié-Gonnardfd6b4cc2013-12-17 13:59:01 +0100218
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200219 if( bind( ctx->fd, cur->ai_addr, MSVC_INT_CAST cur->ai_addrlen ) != 0 )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100220 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200221 close( ctx->fd );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 ret = MBEDTLS_ERR_NET_BIND_FAILED;
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100223 continue;
224 }
225
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100226 /* Listen only makes sense for TCP */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( proto == MBEDTLS_NET_PROTO_TCP )
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100228 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200229 if( listen( ctx->fd, MBEDTLS_NET_LISTEN_BACKLOG ) != 0 )
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +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_LISTEN_FAILED;
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100233 continue;
234 }
Manuel Pégourié-Gonnard389ce632013-12-13 14:00:51 +0100235 }
236
237 /* I we ever get there, it's a success */
238 ret = 0;
239 break;
240 }
241
242 freeaddrinfo( addr_list );
243
244 return( ret );
245
Paul Bakker5121ce52009-01-03 21:22:43 +0000246}
247
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100248#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
249 !defined(EFI32)
Paul Bakker80025412014-01-23 20:59:49 +0100250/*
251 * Check if the requested operation would be blocking on a non-blocking socket
252 * and thus 'failed' with a negative return value.
253 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200254static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100255{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200256 ((void) ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 return( WSAGetLastError() == WSAEWOULDBLOCK );
Paul Bakker80025412014-01-23 20:59:49 +0100258}
Paul Bakker5121ce52009-01-03 21:22:43 +0000259#else
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 *
264 * Note: on a blocking socket this function always returns 0!
265 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200266static int net_would_block( const mbedtls_net_context *ctx )
Paul Bakker80025412014-01-23 20:59:49 +0100267{
268 /*
269 * Never return 'WOULD BLOCK' on a non-blocking socket
270 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200271 if( ( fcntl( ctx->fd, F_GETFL ) & O_NONBLOCK ) != O_NONBLOCK )
Paul Bakker80025412014-01-23 20:59:49 +0100272 return( 0 );
273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 switch( errno )
275 {
276#if defined EAGAIN
277 case EAGAIN:
278#endif
279#if defined EWOULDBLOCK && EWOULDBLOCK != EAGAIN
280 case EWOULDBLOCK:
281#endif
282 return( 1 );
283 }
284 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285}
Paul Bakkerdb20c102014-06-17 14:34:44 +0200286#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288/*
289 * Accept a connection from a remote client
290 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200291int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
292 mbedtls_net_context *client_ctx,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200293 void *client_ip, size_t buf_size, size_t *ip_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000294{
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100295 int ret;
296 int type;
297
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100298 struct sockaddr_storage client_addr;
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
Paul Bakker394c56f2011-12-20 12:19:03 +0000300#if defined(__socklen_t_defined) || defined(_SOCKLEN_T) || \
301 defined(_SOCKLEN_T_DECLARED)
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 socklen_t n = (socklen_t) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100303 socklen_t type_len = (socklen_t) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304#else
305 int n = (int) sizeof( client_addr );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100306 int type_len = (int) sizeof( type );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307#endif
308
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100309 /* Is this a TCP or UDP socket? */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200310 if( getsockopt( bind_ctx->fd, SOL_SOCKET, SO_TYPE,
311 (void *) &type, &type_len ) != 0 ||
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100312 ( type != SOCK_STREAM && type != SOCK_DGRAM ) )
313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100315 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100317 if( type == SOCK_STREAM )
318 {
319 /* TCP: actual accept() */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200320 ret = client_ctx->fd = (int) accept( bind_ctx->fd,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100321 (struct sockaddr *) &client_addr, &n );
322 }
323 else
324 {
325 /* UDP: wait for a message, but keep it in the queue */
326 char buf[1] = { 0 };
327
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200328 ret = recvfrom( bind_ctx->fd, buf, sizeof( buf ), MSG_PEEK,
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100329 (struct sockaddr *) &client_addr, &n );
Manuel Pégourié-Gonnard16a17a42015-06-30 11:20:22 +0200330
331#if defined(_WIN32)
332 if( ret == SOCKET_ERROR &&
333 WSAGetLastError() == WSAEMSGSIZE )
334 {
335 /* We know buf is too small, thanks, just peeking here */
336 ret = 0;
337 }
338#endif
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100339 }
340
341 if( ret < 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200343 if( net_would_block( bind_ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100344 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 }
348
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200349 /* UDP: hijack the listening socket to communicate with the client,
350 * then bind a new socket to accept new connections */
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100351 if( type != SOCK_STREAM )
352 {
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200353 struct sockaddr_storage local_addr;
354 int one = 1;
355
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200356 if( connect( bind_ctx->fd, (struct sockaddr *) &client_addr, n ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 return( MBEDTLS_ERR_NET_ACCEPT_FAILED );
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100358
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200359 client_ctx->fd = bind_ctx->fd;
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200360 bind_ctx->fd = -1; /* In case we exit early */
361
362 n = sizeof( struct sockaddr_storage );
363 if( getsockname( client_ctx->fd,
364 (struct sockaddr *) &local_addr, &n ) != 0 ||
365 ( bind_ctx->fd = (int) socket( local_addr.ss_family,
366 SOCK_DGRAM, IPPROTO_UDP ) ) < 0 ||
367 setsockopt( bind_ctx->fd, SOL_SOCKET, SO_REUSEADDR,
368 (const char *) &one, sizeof( one ) ) != 0 )
369 {
370 return( MBEDTLS_ERR_NET_SOCKET_FAILED );
371 }
372
373 if( bind( bind_ctx->fd, (struct sockaddr *) &local_addr, n ) != 0 )
374 {
375 return( MBEDTLS_ERR_NET_BIND_FAILED );
376 }
Manuel Pégourié-Gonnardf5a13122014-03-23 17:38:16 +0100377 }
378
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 if( client_ip != NULL )
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100380 {
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100381 if( client_addr.ss_family == AF_INET )
382 {
383 struct sockaddr_in *addr4 = (struct sockaddr_in *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200384 *ip_len = sizeof( addr4->sin_addr.s_addr );
385
386 if( buf_size < *ip_len )
387 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
388
389 memcpy( client_ip, &addr4->sin_addr.s_addr, *ip_len );
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100390 }
391 else
392 {
393 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &client_addr;
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200394 *ip_len = sizeof( addr6->sin6_addr.s6_addr );
395
396 if( buf_size < *ip_len )
397 return( MBEDTLS_ERR_NET_BUFFER_TOO_SMALL );
398
399 memcpy( client_ip, &addr6->sin6_addr.s6_addr, *ip_len);
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100400 }
Manuel Pégourié-Gonnard6e315a92013-12-13 16:21:25 +0100401 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403 return( 0 );
404}
405
406/*
407 * Set the socket blocking or non-blocking
408 */
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200409int mbedtls_net_set_block( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000410{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100411#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
412 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000413 u_long n = 0;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200414 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200416 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) & ~O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417#endif
418}
419
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200420int mbedtls_net_set_nonblock( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000421{
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100422#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
423 !defined(EFI32)
Paul Bakkerf4f69682011-04-24 16:08:12 +0000424 u_long n = 1;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200425 return( ioctlsocket( ctx->fd, FIONBIO, &n ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426#else
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200427 return( fcntl( ctx->fd, F_SETFL, fcntl( ctx->fd, F_GETFL ) | O_NONBLOCK ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428#endif
429}
430
431/*
432 * Portable usleep helper
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434void mbedtls_net_usleep( unsigned long usec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000435{
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200436#if defined(_WIN32)
437 Sleep( ( usec + 999 ) / 1000 );
438#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 struct timeval tv;
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100440 tv.tv_sec = usec / 1000000;
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200441#if defined(__unix__) || defined(__unix) || \
442 ( defined(__APPLE__) && defined(__MACH__) )
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100443 tv.tv_usec = (suseconds_t) usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200444#else
Manuel Pégourié-Gonnarde4232462014-11-21 09:52:23 +0100445 tv.tv_usec = usec % 1000000;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 select( 0, NULL, NULL, NULL, &tv );
Manuel Pégourié-Gonnardacecb652015-07-01 11:29:31 +0200448#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000449}
450
451/*
452 * Read at most 'len' characters
453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len )
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100455{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200456 int fd = ((mbedtls_net_context *) ctx)->fd;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200457 int ret = (int) read( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 if( ret < 0 )
460 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200461 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100462 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100464#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
465 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468#else
469 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100473 return( MBEDTLS_ERR_SSL_WANT_READ );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474#endif
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_NET_RECV_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 }
478
479 return( ret );
480}
481
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200482/*
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200483 * Read at most 'len' characters, blocking for at most 'timeout' ms
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200486 uint32_t timeout )
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200487{
488 int ret;
489 struct timeval tv;
490 fd_set read_fds;
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200491 int fd = ((mbedtls_net_context *) ctx)->fd;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200492
493 FD_ZERO( &read_fds );
494 FD_SET( fd, &read_fds );
495
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +0200496 tv.tv_sec = timeout / 1000;
497 tv.tv_usec = ( timeout % 1000 ) * 1000;
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200498
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +0200499 ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200500
501 /* Zero fds ready means we timed out */
502 if( ret == 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100503 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200504
505 if( ret < 0 )
506 {
507#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
508 !defined(EFI32)
509 if( WSAGetLastError() == WSAEINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100510 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200511#else
512 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100513 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200514#endif
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 return( MBEDTLS_ERR_NET_RECV_FAILED );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200517 }
518
519 /* This call will not block */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( mbedtls_net_recv( ctx, buf, len ) );
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200521}
Manuel Pégourié-Gonnard9d9b0032014-09-18 11:22:45 +0200522
Paul Bakker5121ce52009-01-03 21:22:43 +0000523/*
524 * Write at most 'len' characters
525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000527{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200528 int fd = ((mbedtls_net_context *) ctx)->fd;
Sander Niemeijeref5087d2014-08-16 12:45:52 +0200529 int ret = (int) write( fd, buf, len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 if( ret < 0 )
532 {
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200533 if( net_would_block( ctx ) != 0 )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100534 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100536#if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \
537 !defined(EFI32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 if( WSAGetLastError() == WSAECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540#else
541 if( errno == EPIPE || errno == ECONNRESET )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 return( MBEDTLS_ERR_NET_CONN_RESET );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 if( errno == EINTR )
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +0100545 return( MBEDTLS_ERR_SSL_WANT_WRITE );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546#endif
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_NET_SEND_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 }
550
551 return( ret );
552}
553
554/*
555 * Gracefully close the connection
556 */
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200557void mbedtls_net_free( mbedtls_net_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558{
Manuel Pégourié-Gonnard91895852015-06-30 13:34:45 +0200559 if( ctx->fd == -1 )
560 return;
561
562 shutdown( ctx->fd, 2 );
563 close( ctx->fd );
564
565 ctx->fd = -1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000566}
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_NET_C */