blob: 7e8d309f40d2c1d25e20247aa968fe80f78b2f78 [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
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.
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020018 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020020 */
21
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020022/*
23 * Warning: this is an internal utility program we use for tests.
24 * It does break some abstractions from the NET layer, and is thus NOT an
25 * example of good general usage.
26 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000036#else
Simon Butcherd3138c32016-04-27 01:26:50 +010037#include <stdio.h>
38#include <stdlib.h>
39#include <time.h>
40#define mbedtls_time time
41#define mbedtls_time_t time_t
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#define mbedtls_printf printf
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020046int main( void )
47{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020049 return( 0 );
50}
51#else
52
Andres AG788aa4a2016-09-14 14:32:09 +010053#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/error.h"
55#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000056#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020057
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000058#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020059
60/* For select() */
61#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
62 !defined(EFI32)
63#include <winsock2.h>
64#include <windows.h>
65#if defined(_MSC_VER)
66#if defined(_WIN32_WCE)
67#pragma comment( lib, "ws2.lib" )
68#else
69#pragma comment( lib, "ws2_32.lib" )
70#endif
71#endif /* _MSC_VER */
72#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
73#include <sys/time.h>
74#include <sys/types.h>
75#include <unistd.h>
76#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
77
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020078#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020079
80#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020081#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020082#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020083#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010084#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020085
Hanno Becker0cc77742017-10-31 14:10:07 +000086#if defined(MBEDTLS_TIMING_C)
87#define USAGE_PACK \
88 " pack=%%d default: 0 (don't pack)\n" \
89 " options: t > 0 (pack for t milliseconds)\n"
90#else
91#define USAGE_PACK
92#endif
93
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020094#define USAGE \
95 "\n usage: udp_proxy param=<>...\n" \
96 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020097 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020098 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020099 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200100 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200101 "\n" \
102 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200103 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200104 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200105 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200106 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200107 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200108 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200109 " mtu=%%d default: 0 (unlimited)\n" \
110 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200111 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
112 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000113 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200114 "\n" \
115 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000116 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200117 "\n"
118
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200119/*
120 * global options
121 */
122static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200123{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200124 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200125 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200126 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200127 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200128
129 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200130 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200131 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200132 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200133 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200134 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200135 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200136 int protect_len; /* never drop/delay packet of the given size*/
Hanno Becker211f44c2017-10-31 14:08:10 +0000137 int pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100138 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200139 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200140} opt;
141
142static void exit_usage( const char *name, const char *value )
143{
144 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200146 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200150 exit( 1 );
151}
152
153static void get_options( int argc, char *argv[] )
154{
155 int i;
156 char *p, *q;
157
158 opt.server_addr = DFL_SERVER_ADDR;
159 opt.server_port = DFL_SERVER_PORT;
160 opt.listen_addr = DFL_LISTEN_ADDR;
161 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000162 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200163 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200164
165 for( i = 1; i < argc; i++ )
166 {
167 p = argv[i];
168 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200169 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200170 *q++ = '\0';
171
172 if( strcmp( p, "server_addr" ) == 0 )
173 opt.server_addr = q;
174 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200175 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200176 else if( strcmp( p, "listen_addr" ) == 0 )
177 opt.listen_addr = q;
178 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200179 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200180 else if( strcmp( p, "duplicate" ) == 0 )
181 {
182 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200183 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200184 exit_usage( p, q );
185 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200186 else if( strcmp( p, "delay" ) == 0 )
187 {
188 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200189 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200190 exit_usage( p, q );
191 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200192 else if( strcmp( p, "delay_ccs" ) == 0 )
193 {
194 opt.delay_ccs = atoi( q );
195 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
196 exit_usage( p, q );
197 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200198 else if( strcmp( p, "drop" ) == 0 )
199 {
200 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200201 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200202 exit_usage( p, q );
203 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100204 else if( strcmp( p, "pack" ) == 0 )
205 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000206#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000207 opt.pack = atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000208#else
209 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
210 exit( 1 );
211#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100212 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200213 else if( strcmp( p, "mtu" ) == 0 )
214 {
215 opt.mtu = atoi( q );
216 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
217 exit_usage( p, q );
218 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200219 else if( strcmp( p, "bad_ad" ) == 0 )
220 {
221 opt.bad_ad = atoi( q );
222 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
223 exit_usage( p, q );
224 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200225 else if( strcmp( p, "protect_hvr" ) == 0 )
226 {
227 opt.protect_hvr = atoi( q );
228 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
229 exit_usage( p, q );
230 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200231 else if( strcmp( p, "protect_len" ) == 0 )
232 {
233 opt.protect_len = atoi( q );
234 if( opt.protect_len < 0 )
235 exit_usage( p, q );
236 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200237 else if( strcmp( p, "seed" ) == 0 )
238 {
239 opt.seed = atoi( q );
240 if( opt.seed == 0 )
241 exit_usage( p, q );
242 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200243 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200244 exit_usage( p, NULL );
245 }
246}
247
248static const char *msg_type( unsigned char *msg, size_t len )
249{
250 if( len < 1 ) return( "Invalid" );
251 switch( msg[0] )
252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
254 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
255 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
256 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200257 default: return( "Unknown" );
258 }
259
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200260 if( len < 13 + 12 ) return( "Invalid handshake" );
261
262 /*
263 * Our handshake message are less than 2^16 bytes long, so they should
264 * have 0 as the first byte of length, frag_offset and frag_length.
265 * Otherwise, assume they are encrypted.
266 */
267 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
268
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200269 switch( msg[13] )
270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
272 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
273 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
274 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
275 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
276 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
277 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
278 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
279 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
280 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
281 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
282 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200283 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200284 }
285}
286
Hanno Becker0cc77742017-10-31 14:10:07 +0000287#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200288/* Return elapsed time in milliseconds since the first call */
289static unsigned long ellapsed_time( void )
290{
Hanno Becker92474da2017-10-31 14:09:30 +0000291 static int initialized = 0;
292 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200293
Hanno Becker92474da2017-10-31 14:09:30 +0000294 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200295 {
Hanno Becker92474da2017-10-31 14:09:30 +0000296 (void) mbedtls_timing_get_timer( &hires, 1 );
297 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200298 return( 0 );
299 }
300
Hanno Becker92474da2017-10-31 14:09:30 +0000301 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200302}
303
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200304typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200305{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100306 mbedtls_net_context *ctx;
307
308 const char *description;
309
310 unsigned long packet_lifetime;
311 size_t num_datagrams;
312
313 unsigned char data[MAX_MSG_SIZE];
314 unsigned len;
315
316} ctx_buffer;
317
318static ctx_buffer outbuf[2];
319
320static int ctx_buffer_flush( ctx_buffer *buf )
321{
322 int ret;
323
Hanno Beckerdf4180a2017-10-27 13:43:58 +0100324 mbedtls_printf( " %05lu flush %s: %u bytes, %lu datagrams, last %ld ms\n",
325 ellapsed_time(), buf->description, buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100326 ellapsed_time() - buf->packet_lifetime );
327
328 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
329
330 buf->len = 0;
331 buf->num_datagrams = 0;
332
333 return( ret );
334}
335
336static inline int ctx_buffer_check( ctx_buffer *buf )
337{
338 if( buf->len > 0 &&
Hanno Becker211f44c2017-10-31 14:08:10 +0000339 ellapsed_time() - buf->packet_lifetime >= (size_t) opt.pack )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100340 {
341 return( ctx_buffer_flush( buf ) );
342 }
343
344 return( 0 );
345}
346
347static int ctx_buffer_append( ctx_buffer *buf,
348 const unsigned char * data,
349 size_t len )
350{
351 int ret;
352
353 if( len > sizeof( buf->data ) )
354 {
355 mbedtls_printf( " ! buffer size %lu too large (max %lu)\n",
356 len, sizeof( buf->data ) );
357 return( -1 );
358 }
359
360 if( sizeof( buf->data ) - buf->len < len )
361 {
362 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
363 return( ret );
364 }
365
366 memcpy( buf->data + buf->len, data, len );
367
368 buf->len += len;
369 if( ++buf->num_datagrams == 1 )
370 buf->packet_lifetime = ellapsed_time();
371
372 return( len );
373}
374
375static int dispatch_data( mbedtls_net_context *ctx,
376 const unsigned char * data,
377 size_t len )
378{
379 ctx_buffer *buf = NULL;
Hanno Becker0cc77742017-10-31 14:10:07 +0000380
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100381 if( outbuf[0].ctx == ctx )
382 buf = &outbuf[0];
383 else if( outbuf[1].ctx == ctx )
384 buf = &outbuf[1];
385
386 if( buf == NULL )
387 return( mbedtls_net_send( ctx, data, len ) );
388
389 return( ctx_buffer_append( buf, data, len ) );
390}
391
Hanno Becker0cc77742017-10-31 14:10:07 +0000392#else /* MBEDTLS_TIMING_C */
393
394static int dispatch_data( mbedtls_net_context *ctx,
395 const unsigned char * data,
396 size_t len )
397{
398 return( mbedtls_net_send( ctx, data, len ) );
399}
400
401#endif /* MBEDTLS_TIMING_C */
402
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100403typedef struct
404{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200405 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200406 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200407 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200408 unsigned len;
409 unsigned char buf[MAX_MSG_SIZE];
410} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200411
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200412/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
413void print_packet( const packet *p, const char *why )
414{
Hanno Becker0cc77742017-10-31 14:10:07 +0000415#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200416 if( why == NULL )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100417 mbedtls_printf( " %05lu dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200418 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200419 else
Hanno Becker0cc77742017-10-31 14:10:07 +0000420 mbedtls_printf( " %05lu dispatch %s %s (%u bytes): %s\n",
421 ellapsed_time(), p->way, p->type, p->len, why );
422#else
423 if( why == NULL )
424 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
425 p->way, p->type, p->len );
426 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100427 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200428 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000429#endif
430
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200431 fflush( stdout );
432}
433
434int send_packet( const packet *p, const char *why )
435{
436 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200437 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200438
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200439 /* insert corrupted ApplicationData record? */
440 if( opt.bad_ad &&
441 strcmp( p->type, "ApplicationData" ) == 0 )
442 {
443 unsigned char buf[MAX_MSG_SIZE];
444 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200445
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100446 if( p->len <= 13 )
447 {
448 mbedtls_printf( " ! can't corrupt empty AD record" );
449 }
450 else
451 {
452 ++buf[13];
453 print_packet( p, "corrupted" );
454 }
455
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100456 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200457 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100458 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200459 return( ret );
460 }
461 }
462
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200463 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100464 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200465 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100466 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200467 return( ret );
468 }
469
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200470 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200471 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200472 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200473 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200474 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200475 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200476
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100477 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200478 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100479 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200480 return( ret );
481 }
482 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200483
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200484 return( 0 );
485}
486
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200487static packet prev;
488
489void clear_pending( void )
490{
491 memset( &prev, 0, sizeof( packet ) );
492}
493
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200494/*
495 * Avoid dropping or delaying a packet that was already dropped twice: this
496 * only results in uninteresting timeouts. We can't rely on type to identify
497 * packets, since during renegotiation they're all encrypted. So, rely on
498 * size mod 2048 (which is usually just size).
499 */
500static unsigned char dropped[2048] = { 0 };
501#define DROP_MAX 2
502
503/*
504 * OpenSSL groups packets in a datagram the first time it sends them, but not
505 * when it resends them. Count every record as seen the first time.
506 */
507void update_dropped( const packet *p )
508{
509 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200510 const unsigned char *end = p->buf + p->len;
511 const unsigned char *cur = p->buf;
512 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
513
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200514 ++dropped[id];
515
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200516 /* Avoid counting single record twice */
517 if( len == p->len )
518 return;
519
520 while( cur < end )
521 {
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200522 len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200523
524 id = len % sizeof( dropped );
525 ++dropped[id];
526
527 cur += len;
528 }
529}
530
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200531int handle_message( const char *way,
532 mbedtls_net_context *dst,
533 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200534{
535 int ret;
536 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200537 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200538
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200539 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200540 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200541 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200543 return( ret );
544 }
545
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200546 cur.len = ret;
547 cur.type = msg_type( cur.buf, cur.len );
548 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200549 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200550 print_packet( &cur, NULL );
551
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200552 id = cur.len % sizeof( dropped );
553
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200554 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200555 if( ( opt.mtu != 0 &&
556 cur.len > (unsigned) opt.mtu ) ||
557 ( opt.drop != 0 &&
558 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200559 ! ( opt.protect_hvr &&
560 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200561 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200562 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200563 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200564 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200565 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200566 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200567 else if( ( opt.delay_ccs == 1 &&
568 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
569 ( opt.delay != 0 &&
570 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200571 ! ( opt.protect_hvr &&
572 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200573 prev.dst == NULL &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200574 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200575 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200576 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200577 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200578 memcpy( &prev, &cur, sizeof( packet ) );
579 }
580 else
581 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200582 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200583 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
584 return( ret );
585
586 /* send previously delayed message if any */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200587 if( prev.dst != NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200588 {
589 ret = send_packet( &prev, "delayed" );
590 memset( &prev, 0, sizeof( packet ) );
591 if( ret != 0 )
592 return( ret );
593 }
594 }
595
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200596 return( 0 );
597}
598
599int main( int argc, char *argv[] )
600{
601 int ret;
602
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200603 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100604 struct timeval tm;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200605
606 int nb_fds;
607 fd_set read_fds;
608
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100609 tm.tv_sec = 0;
610 tm.tv_usec = 0;
611
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200612 mbedtls_net_init( &listen_fd );
613 mbedtls_net_init( &client_fd );
614 mbedtls_net_init( &server_fd );
615
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200616 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200617
618 /*
619 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
620 * exactly 1 in N packets would lead to problems when a flight has exactly
621 * N packets: the same packet would be dropped on every resend.
622 *
623 * In order to be able to reproduce problems reliably, the seed may be
624 * specified explicitly.
625 */
626 if( opt.seed == 0 )
627 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200628 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200630 }
631
632 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200633
634 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200635 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200636 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200637 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200638 opt.server_addr, opt.server_port );
639 fflush( stdout );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
642 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200645 goto exit;
646 }
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200649
650 /*
651 * 1. Setup the "listening" UDP socket
652 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200653 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200654 opt.listen_addr, opt.listen_port );
655 fflush( stdout );
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
658 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200661 goto exit;
662 }
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200665
666 /*
667 * 2. Wait until a client connects
668 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200669accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200670 mbedtls_net_free( &client_fd );
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200673 fflush( stdout );
674
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200675 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200676 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200679 goto exit;
680 }
681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200683
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200684 /*
685 * 3. Forward packets forever (kill the process to terminate it)
686 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200687 clear_pending();
688 memset( dropped, 0, sizeof( dropped ) );
689
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200690 nb_fds = client_fd.fd;
691 if( nb_fds < server_fd.fd )
692 nb_fds = server_fd.fd;
693 if( nb_fds < listen_fd.fd )
694 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200695 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200696
Hanno Becker0cc77742017-10-31 14:10:07 +0000697#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000698 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100699 {
700 outbuf[0].ctx = &server_fd;
701 outbuf[0].description = "S <- C";
702 outbuf[0].num_datagrams = 0;
703 outbuf[0].len = 0;
704
705 outbuf[1].ctx = &client_fd;
706 outbuf[1].description = "S -> C";
707 outbuf[1].num_datagrams = 0;
708 outbuf[1].len = 0;
709 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000710 else
711 {
712 outbuf[0].ctx = NULL;
713 outbuf[1].ctx = NULL;
714 }
715#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100716
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200717 while( 1 )
718 {
719 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200720 FD_SET( server_fd.fd, &read_fds );
721 FD_SET( client_fd.fd, &read_fds );
722 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200723
Hanno Becker0cc77742017-10-31 14:10:07 +0000724#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100725 ctx_buffer_check( &outbuf[0] );
726 ctx_buffer_check( &outbuf[1] );
Hanno Becker0cc77742017-10-31 14:10:07 +0000727#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100728
729 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, &tm ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200730 {
731 perror( "select" );
732 goto exit;
733 }
734
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200735 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200736 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200737
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200738 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200739 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200740 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200741 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200742 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200743 }
744
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200745 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200746 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200747 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200748 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200749 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200750 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100751
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200752 }
753
754exit:
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#ifdef MBEDTLS_ERROR_C
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200757 if( ret != 0 )
758 {
759 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_strerror( ret, error_buf, 100 );
761 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200762 fflush( stdout );
763 }
764#endif
765
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200766 mbedtls_net_free( &client_fd );
767 mbedtls_net_free( &server_fd );
768 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200769
770#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200772 fflush( stdout ); getchar();
773#endif
774
775 return( ret != 0 );
776}
777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#endif /* MBEDTLS_NET_C */