blob: 55e0f249cecb5c38bb83f01489365d43a4859bc9 [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>
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010040#define mbedtls_time time
41#define mbedtls_time_t time_t
42#define mbedtls_printf printf
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010043#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia80081a62018-04-29 21:58:53 +010044#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
45#endif /* MBEDTLS_PLATFORM_C */
Manuel Pégourié-Gonnardf2246782015-01-29 13:29:20 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_NET_C)
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020048int main( void )
49{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf( "MBEDTLS_NET_C not defined.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020051 return( 0 );
52}
53#else
54
Andres AG788aa4a2016-09-14 14:32:09 +010055#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/error.h"
57#include "mbedtls/ssl.h"
Hanno Becker0cc77742017-10-31 14:10:07 +000058#include "mbedtls/timing.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020059
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000060#include <string.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020061
62/* For select() */
63#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
64 !defined(EFI32)
65#include <winsock2.h>
66#include <windows.h>
67#if defined(_MSC_VER)
68#if defined(_WIN32_WCE)
69#pragma comment( lib, "ws2.lib" )
70#else
71#pragma comment( lib, "ws2_32.lib" )
72#endif
73#endif /* _MSC_VER */
74#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
75#include <sys/time.h>
76#include <sys/types.h>
77#include <unistd.h>
78#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
79
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020080#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020081
82#define DFL_SERVER_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020083#define DFL_SERVER_PORT "4433"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020084#define DFL_LISTEN_ADDR "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020085#define DFL_LISTEN_PORT "5556"
Hanno Becker1dd62ea2017-05-22 14:30:59 +010086#define DFL_PACK 0
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020087
Hanno Becker0cc77742017-10-31 14:10:07 +000088#if defined(MBEDTLS_TIMING_C)
89#define USAGE_PACK \
90 " pack=%%d default: 0 (don't pack)\n" \
91 " options: t > 0 (pack for t milliseconds)\n"
92#else
93#define USAGE_PACK
94#endif
95
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020096#define USAGE \
97 "\n usage: udp_proxy param=<>...\n" \
98 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020099 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200100 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200101 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200102 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200103 "\n" \
104 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200105 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200106 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200107 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200108 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200109 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200110 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200111 " mtu=%%d default: 0 (unlimited)\n" \
112 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200113 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
114 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000115 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200116 "\n" \
117 " seed=%%d default: (use current time)\n" \
Hanno Becker0cc77742017-10-31 14:10:07 +0000118 USAGE_PACK \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200119 "\n"
120
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200121/*
122 * global options
123 */
124static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200125{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200126 const char *server_addr; /* address to forward packets to */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200127 const char *server_port; /* port to forward packets to */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200128 const char *listen_addr; /* address for accepting client connections */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200129 const char *listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200130
131 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200132 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200133 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200134 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200135 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200136 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200137 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200138 int protect_len; /* never drop/delay packet of the given size*/
Hanno Becker77abef52017-11-02 10:50:28 +0000139 unsigned pack; /* merge packets into single datagram for
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100140 * at most \c merge milliseconds if > 0 */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200141 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200142} opt;
143
144static void exit_usage( const char *name, const char *value )
145{
146 if( value == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_printf( " option %s: illegal value: %s\n", name, value );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( USAGE );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200152 exit( 1 );
153}
154
155static void get_options( int argc, char *argv[] )
156{
157 int i;
158 char *p, *q;
159
160 opt.server_addr = DFL_SERVER_ADDR;
161 opt.server_port = DFL_SERVER_PORT;
162 opt.listen_addr = DFL_LISTEN_ADDR;
163 opt.listen_port = DFL_LISTEN_PORT;
Hanno Becker211f44c2017-10-31 14:08:10 +0000164 opt.pack = DFL_PACK;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200165 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200166
167 for( i = 1; i < argc; i++ )
168 {
169 p = argv[i];
170 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200171 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200172 *q++ = '\0';
173
174 if( strcmp( p, "server_addr" ) == 0 )
175 opt.server_addr = q;
176 else if( strcmp( p, "server_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200177 opt.server_port = q;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200178 else if( strcmp( p, "listen_addr" ) == 0 )
179 opt.listen_addr = q;
180 else if( strcmp( p, "listen_port" ) == 0 )
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200181 opt.listen_port = q;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200182 else if( strcmp( p, "duplicate" ) == 0 )
183 {
184 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200185 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200186 exit_usage( p, q );
187 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200188 else if( strcmp( p, "delay" ) == 0 )
189 {
190 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200191 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200192 exit_usage( p, q );
193 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200194 else if( strcmp( p, "delay_ccs" ) == 0 )
195 {
196 opt.delay_ccs = atoi( q );
197 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
198 exit_usage( p, q );
199 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200200 else if( strcmp( p, "drop" ) == 0 )
201 {
202 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200203 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200204 exit_usage( p, q );
205 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100206 else if( strcmp( p, "pack" ) == 0 )
207 {
Hanno Becker0cc77742017-10-31 14:10:07 +0000208#if defined(MBEDTLS_TIMING_C)
Hanno Becker77abef52017-11-02 10:50:28 +0000209 opt.pack = (unsigned) atoi( q );
Hanno Becker0cc77742017-10-31 14:10:07 +0000210#else
211 mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" );
212 exit( 1 );
213#endif
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100214 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200215 else if( strcmp( p, "mtu" ) == 0 )
216 {
217 opt.mtu = atoi( q );
218 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
219 exit_usage( p, q );
220 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200221 else if( strcmp( p, "bad_ad" ) == 0 )
222 {
223 opt.bad_ad = atoi( q );
224 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
225 exit_usage( p, q );
226 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200227 else if( strcmp( p, "protect_hvr" ) == 0 )
228 {
229 opt.protect_hvr = atoi( q );
230 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
231 exit_usage( p, q );
232 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200233 else if( strcmp( p, "protect_len" ) == 0 )
234 {
235 opt.protect_len = atoi( q );
236 if( opt.protect_len < 0 )
237 exit_usage( p, q );
238 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200239 else if( strcmp( p, "seed" ) == 0 )
240 {
241 opt.seed = atoi( q );
242 if( opt.seed == 0 )
243 exit_usage( p, q );
244 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200245 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200246 exit_usage( p, NULL );
247 }
248}
249
250static const char *msg_type( unsigned char *msg, size_t len )
251{
252 if( len < 1 ) return( "Invalid" );
253 switch( msg[0] )
254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
256 case MBEDTLS_SSL_MSG_ALERT: return( "Alert" );
257 case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
258 case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200259 default: return( "Unknown" );
260 }
261
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200262 if( len < 13 + 12 ) return( "Invalid handshake" );
263
264 /*
265 * Our handshake message are less than 2^16 bytes long, so they should
266 * have 0 as the first byte of length, frag_offset and frag_length.
267 * Otherwise, assume they are encrypted.
268 */
269 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
270
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200271 switch( msg[13] )
272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
274 case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" );
275 case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" );
276 case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
277 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
278 case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" );
279 case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
280 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
281 case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
282 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
283 case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
284 case MBEDTLS_SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200285 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200286 }
287}
288
Hanno Becker0cc77742017-10-31 14:10:07 +0000289#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200290/* Return elapsed time in milliseconds since the first call */
Hanno Becker77abef52017-11-02 10:50:28 +0000291static unsigned ellapsed_time( void )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200292{
Hanno Becker92474da2017-10-31 14:09:30 +0000293 static int initialized = 0;
294 static struct mbedtls_timing_hr_time hires;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200295
Hanno Becker92474da2017-10-31 14:09:30 +0000296 if( initialized == 0 )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200297 {
Hanno Becker92474da2017-10-31 14:09:30 +0000298 (void) mbedtls_timing_get_timer( &hires, 1 );
299 initialized = 1;
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200300 return( 0 );
301 }
302
Hanno Becker92474da2017-10-31 14:09:30 +0000303 return( mbedtls_timing_get_timer( &hires, 0 ) );
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200304}
305
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200306typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200307{
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100308 mbedtls_net_context *ctx;
309
310 const char *description;
311
Hanno Becker77abef52017-11-02 10:50:28 +0000312 unsigned packet_lifetime;
313 unsigned num_datagrams;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100314
315 unsigned char data[MAX_MSG_SIZE];
Hanno Beckera5e68972017-12-06 08:35:02 +0000316 size_t len;
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100317
318} ctx_buffer;
319
320static ctx_buffer outbuf[2];
321
322static int ctx_buffer_flush( ctx_buffer *buf )
323{
324 int ret;
325
Hanno Becker77abef52017-11-02 10:50:28 +0000326 mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n",
327 ellapsed_time(), buf->description,
Hanno Beckera5e68972017-12-06 08:35:02 +0000328 (unsigned) buf->len, buf->num_datagrams,
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100329 ellapsed_time() - buf->packet_lifetime );
330
331 ret = mbedtls_net_send( buf->ctx, buf->data, buf->len );
332
333 buf->len = 0;
334 buf->num_datagrams = 0;
335
336 return( ret );
337}
338
Hanno Becker77abef52017-11-02 10:50:28 +0000339static unsigned ctx_buffer_time_remaining( ctx_buffer *buf )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100340{
Hanno Becker77abef52017-11-02 10:50:28 +0000341 unsigned const cur_time = ellapsed_time();
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100342
Hanno Becker77abef52017-11-02 10:50:28 +0000343 if( buf->num_datagrams == 0 )
344 return( (unsigned) -1 );
345
346 if( cur_time - buf->packet_lifetime >= opt.pack )
347 return( 0 );
348
349 return( opt.pack - ( cur_time - buf->packet_lifetime ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100350}
351
352static int ctx_buffer_append( ctx_buffer *buf,
353 const unsigned char * data,
354 size_t len )
355{
356 int ret;
357
Hanno Beckera5e68972017-12-06 08:35:02 +0000358 if( len > (size_t) INT_MAX )
359 return( -1 );
360
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100361 if( len > sizeof( buf->data ) )
362 {
Hanno Becker77abef52017-11-02 10:50:28 +0000363 mbedtls_printf( " ! buffer size %u too large (max %u)\n",
364 (unsigned) len, (unsigned) sizeof( buf->data ) );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100365 return( -1 );
366 }
367
368 if( sizeof( buf->data ) - buf->len < len )
369 {
370 if( ( ret = ctx_buffer_flush( buf ) ) <= 0 )
371 return( ret );
372 }
373
374 memcpy( buf->data + buf->len, data, len );
375
376 buf->len += len;
377 if( ++buf->num_datagrams == 1 )
378 buf->packet_lifetime = ellapsed_time();
379
Hanno Beckera5e68972017-12-06 08:35:02 +0000380 return( (int) len );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100381}
Hanno Becker77abef52017-11-02 10:50:28 +0000382#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100383
384static int dispatch_data( mbedtls_net_context *ctx,
385 const unsigned char * data,
386 size_t len )
387{
Hanno Becker77abef52017-11-02 10:50:28 +0000388#if defined(MBEDTLS_TIMING_C)
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100389 ctx_buffer *buf = NULL;
Hanno Becker77abef52017-11-02 10:50:28 +0000390 if( opt.pack > 0 )
391 {
392 if( outbuf[0].ctx == ctx )
393 buf = &outbuf[0];
394 else if( outbuf[1].ctx == ctx )
395 buf = &outbuf[1];
Hanno Becker0cc77742017-10-31 14:10:07 +0000396
Hanno Becker77abef52017-11-02 10:50:28 +0000397 if( buf == NULL )
398 return( -1 );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100399
Hanno Becker77abef52017-11-02 10:50:28 +0000400 return( ctx_buffer_append( buf, data, len ) );
401 }
402#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100403
Hanno Becker0cc77742017-10-31 14:10:07 +0000404 return( mbedtls_net_send( ctx, data, len ) );
405}
406
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100407typedef struct
408{
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200409 mbedtls_net_context *dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200410 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200411 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200412 unsigned len;
413 unsigned char buf[MAX_MSG_SIZE];
414} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200415
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200416/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
417void print_packet( const packet *p, const char *why )
418{
Hanno Becker0cc77742017-10-31 14:10:07 +0000419#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200420 if( why == NULL )
Hanno Becker77abef52017-11-02 10:50:28 +0000421 mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200422 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200423 else
Hanno Becker77abef52017-11-02 10:50:28 +0000424 mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n",
Hanno Becker0cc77742017-10-31 14:10:07 +0000425 ellapsed_time(), p->way, p->type, p->len, why );
426#else
427 if( why == NULL )
428 mbedtls_printf( " dispatch %s %s (%u bytes)\n",
429 p->way, p->type, p->len );
430 else
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100431 mbedtls_printf( " dispatch %s %s (%u bytes): %s\n",
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200432 p->way, p->type, p->len, why );
Hanno Becker0cc77742017-10-31 14:10:07 +0000433#endif
434
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200435 fflush( stdout );
436}
437
438int send_packet( const packet *p, const char *why )
439{
440 int ret;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200441 mbedtls_net_context *dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200442
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200443 /* insert corrupted ApplicationData record? */
444 if( opt.bad_ad &&
445 strcmp( p->type, "ApplicationData" ) == 0 )
446 {
447 unsigned char buf[MAX_MSG_SIZE];
448 memcpy( buf, p->buf, p->len );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200449
Hanno Beckerfbb0b702017-05-26 16:55:07 +0100450 if( p->len <= 13 )
451 {
452 mbedtls_printf( " ! can't corrupt empty AD record" );
453 }
454 else
455 {
456 ++buf[13];
457 print_packet( p, "corrupted" );
458 }
459
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100460 if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200461 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100462 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200463 return( ret );
464 }
465 }
466
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200467 print_packet( p, why );
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100468 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200469 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100470 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200471 return( ret );
472 }
473
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200474 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200475 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200476 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200477 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200478 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200479 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200480
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100481 if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200482 {
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100483 mbedtls_printf( " ! dispatch returned %d\n", ret );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200484 return( ret );
485 }
486 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200487
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200488 return( 0 );
489}
490
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200491static packet prev;
492
493void clear_pending( void )
494{
495 memset( &prev, 0, sizeof( packet ) );
496}
497
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200498/*
499 * Avoid dropping or delaying a packet that was already dropped twice: this
500 * only results in uninteresting timeouts. We can't rely on type to identify
501 * packets, since during renegotiation they're all encrypted. So, rely on
502 * size mod 2048 (which is usually just size).
503 */
504static unsigned char dropped[2048] = { 0 };
505#define DROP_MAX 2
506
507/*
508 * OpenSSL groups packets in a datagram the first time it sends them, but not
509 * when it resends them. Count every record as seen the first time.
510 */
511void update_dropped( const packet *p )
512{
513 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200514 const unsigned char *end = p->buf + p->len;
515 const unsigned char *cur = p->buf;
516 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
517
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200518 ++dropped[id];
519
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200520 /* Avoid counting single record twice */
521 if( len == p->len )
522 return;
523
524 while( cur < end )
525 {
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200526 len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200527
528 id = len % sizeof( dropped );
529 ++dropped[id];
530
531 cur += len;
532 }
533}
534
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200535int handle_message( const char *way,
536 mbedtls_net_context *dst,
537 mbedtls_net_context *src )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200538{
539 int ret;
540 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200541 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200542
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200543 /* receive packet */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200544 if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200547 return( ret );
548 }
549
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200550 cur.len = ret;
551 cur.type = msg_type( cur.buf, cur.len );
552 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200553 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200554 print_packet( &cur, NULL );
555
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200556 id = cur.len % sizeof( dropped );
557
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200558 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200559 if( ( opt.mtu != 0 &&
560 cur.len > (unsigned) opt.mtu ) ||
561 ( opt.drop != 0 &&
562 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200563 ! ( opt.protect_hvr &&
564 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200565 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200566 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200567 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200568 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200569 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200570 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200571 else if( ( opt.delay_ccs == 1 &&
572 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
573 ( opt.delay != 0 &&
574 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200575 ! ( opt.protect_hvr &&
576 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200577 prev.dst == NULL &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200578 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200579 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200580 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200581 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200582 memcpy( &prev, &cur, sizeof( packet ) );
583 }
584 else
585 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200586 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200587 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
588 return( ret );
589
590 /* send previously delayed message if any */
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200591 if( prev.dst != NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200592 {
593 ret = send_packet( &prev, "delayed" );
594 memset( &prev, 0, sizeof( packet ) );
595 if( ret != 0 )
596 return( ret );
597 }
598 }
599
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200600 return( 0 );
601}
602
603int main( int argc, char *argv[] )
604{
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100605 int ret = 1;
606 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200607
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200608 mbedtls_net_context listen_fd, client_fd, server_fd;
Hanno Becker77abef52017-11-02 10:50:28 +0000609
610#if defined( MBEDTLS_TIMING_C )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100611 struct timeval tm;
Hanno Becker77abef52017-11-02 10:50:28 +0000612#endif
613
614 struct timeval *tm_ptr = NULL;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200615
616 int nb_fds;
617 fd_set read_fds;
618
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200619 mbedtls_net_init( &listen_fd );
620 mbedtls_net_init( &client_fd );
621 mbedtls_net_init( &server_fd );
622
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200623 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200624
625 /*
626 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
627 * exactly 1 in N packets would lead to problems when a flight has exactly
628 * N packets: the same packet would be dropped on every resend.
629 *
630 * In order to be able to reproduce problems reliably, the seed may be
631 * specified explicitly.
632 */
633 if( opt.seed == 0 )
634 {
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +0200635 opt.seed = (unsigned int) time( NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200637 }
638
639 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200640
641 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200642 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200643 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200644 mbedtls_printf( " . Connect to server on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200645 opt.server_addr, opt.server_port );
646 fflush( stdout );
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port,
649 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200652 goto exit;
653 }
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200656
657 /*
658 * 1. Setup the "listening" UDP socket
659 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200660 mbedtls_printf( " . Bind on UDP/%s/%s ...",
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200661 opt.listen_addr, opt.listen_port );
662 fflush( stdout );
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
665 MBEDTLS_NET_PROTO_UDP ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200668 goto exit;
669 }
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200672
673 /*
674 * 2. Wait until a client connects
675 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200676accept:
Manuel Pégourié-Gonnardabc729e2015-07-01 01:28:24 +0200677 mbedtls_net_free( &client_fd );
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_printf( " . Waiting for a remote connection ..." );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200680 fflush( stdout );
681
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200682 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200683 NULL, 0, NULL ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200686 goto exit;
687 }
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_printf( " ok\n" );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200690
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200691 /*
692 * 3. Forward packets forever (kill the process to terminate it)
693 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200694 clear_pending();
695 memset( dropped, 0, sizeof( dropped ) );
696
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200697 nb_fds = client_fd.fd;
698 if( nb_fds < server_fd.fd )
699 nb_fds = server_fd.fd;
700 if( nb_fds < listen_fd.fd )
701 nb_fds = listen_fd.fd;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200702 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200703
Hanno Becker0cc77742017-10-31 14:10:07 +0000704#if defined(MBEDTLS_TIMING_C)
Hanno Becker211f44c2017-10-31 14:08:10 +0000705 if( opt.pack > 0 )
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100706 {
707 outbuf[0].ctx = &server_fd;
708 outbuf[0].description = "S <- C";
709 outbuf[0].num_datagrams = 0;
710 outbuf[0].len = 0;
711
712 outbuf[1].ctx = &client_fd;
713 outbuf[1].description = "S -> C";
714 outbuf[1].num_datagrams = 0;
715 outbuf[1].len = 0;
716 }
Hanno Becker0cc77742017-10-31 14:10:07 +0000717#endif /* MBEDTLS_TIMING_C */
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100718
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200719 while( 1 )
720 {
Hanno Becker77abef52017-11-02 10:50:28 +0000721#if defined(MBEDTLS_TIMING_C)
722 if( opt.pack > 0 )
723 {
724 unsigned max_wait_server, max_wait_client, max_wait;
725 max_wait_server = ctx_buffer_time_remaining( &outbuf[0] );
726 max_wait_client = ctx_buffer_time_remaining( &outbuf[1] );
727
728 max_wait = (unsigned) -1;
729
730 if( max_wait_server == 0 )
731 ctx_buffer_flush( &outbuf[0] );
732 else
733 max_wait = max_wait_server;
734
735 if( max_wait_client == 0 )
736 ctx_buffer_flush( &outbuf[1] );
737 else
738 {
739 if( max_wait_client < max_wait )
740 max_wait = max_wait_client;
741 }
742
743 if( max_wait != (unsigned) -1 )
744 {
745 tm.tv_sec = max_wait / 1000;
746 tm.tv_usec = ( max_wait % 1000 ) * 1000;
747
748 tm_ptr = &tm;
749 }
750 else
751 {
752 tm_ptr = NULL;
753 }
754 }
755#endif /* MBEDTLS_TIMING_C */
756
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200757 FD_ZERO( &read_fds );
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200758 FD_SET( server_fd.fd, &read_fds );
759 FD_SET( client_fd.fd, &read_fds );
760 FD_SET( listen_fd.fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200761
Hanno Becker77abef52017-11-02 10:50:28 +0000762 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200763 {
764 perror( "select" );
765 goto exit;
766 }
767
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200768 if( FD_ISSET( listen_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200769 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200770
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200771 if( FD_ISSET( client_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200772 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200773 if( ( ret = handle_message( "S <- C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200774 &server_fd, &client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200775 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200776 }
777
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200778 if( FD_ISSET( server_fd.fd, &read_fds ) )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200779 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200780 if( ( ret = handle_message( "S -> C",
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200781 &client_fd, &server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200782 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200783 }
Hanno Becker1dd62ea2017-05-22 14:30:59 +0100784
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200785 }
786
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100787 exit_code = MBEDTLS_EXIT_SUCCESS;
788
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200789exit:
790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#ifdef MBEDTLS_ERROR_C
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100792 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200793 {
794 char error_buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_strerror( ret, error_buf, 100 );
796 mbedtls_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200797 fflush( stdout );
798 }
799#endif
800
Manuel Pégourié-Gonnard3d7d00a2015-06-30 15:55:03 +0200801 mbedtls_net_free( &client_fd );
802 mbedtls_net_free( &server_fd );
803 mbedtls_net_free( &listen_fd );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200804
805#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 mbedtls_printf( " Press Enter to exit this program.\n" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200807 fflush( stdout ); getchar();
808#endif
809
Andres Amaya Garcia80081a62018-04-29 21:58:53 +0100810 return( exit_code );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200811}
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_NET_C */