blob: c95b7f34aa84834b154f2483fb46582682daf4ea [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 *
4 * Copyright (C) 2006-2014, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#if !defined(POLARSSL_CONFIG_FILE)
27#include "polarssl/config.h"
28#else
29#include POLARSSL_CONFIG_FILE
30#endif
31
32#if !defined(POLARSSL_NET_C)
33#include <stdio.h>
34int main( void )
35{
36 printf( "POLARSSL_NET_C not defined.\n" );
37 return( 0 );
38}
39#else
40
41#include "polarssl/net.h"
42#include "polarssl/error.h"
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020043#include "polarssl/ssl.h"
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020044
45#include <stdio.h>
46#include <stdlib.h>
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020047#include <time.h>
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020048
49/* For select() */
50#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
51 !defined(EFI32)
52#include <winsock2.h>
53#include <windows.h>
54#if defined(_MSC_VER)
55#if defined(_WIN32_WCE)
56#pragma comment( lib, "ws2.lib" )
57#else
58#pragma comment( lib, "ws2_32.lib" )
59#endif
60#endif /* _MSC_VER */
61#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
62#include <sys/time.h>
63#include <sys/types.h>
64#include <unistd.h>
65#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
66
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +020067/* For gettimeofday() */
68#if !defined(_WIN32)
69#include <sys/time.h>
70#endif
71
Manuel Pégourié-Gonnardb46780e2014-09-23 12:17:30 +020072#define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020073
74#define DFL_SERVER_ADDR "localhost"
75#define DFL_SERVER_PORT 4433
76#define DFL_LISTEN_ADDR "localhost"
77#define DFL_LISTEN_PORT 5556
78
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020079#define USAGE \
80 "\n usage: udp_proxy param=<>...\n" \
81 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020082 " server_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020083 " server_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020084 " listen_addr=%%s default: localhost\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020085 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020086 "\n" \
87 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020088 " duplicate about 1:N packets randomly\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020089 " delay=%%d default: 0 (no delayed packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020090 " delay about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020091 " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +020092 " drop=%%d default: 0 (no dropped packets)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020093 " drop about 1:N packets randomly\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +020094 " mtu=%%d default: 0 (unlimited)\n" \
95 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +020096 " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \
97 " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020098 " protect_len=%%d default: (don't protect packets of this size)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020099 "\n" \
100 " seed=%%d default: (use current time)\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200101 "\n"
102
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200103/*
104 * global options
105 */
106static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200107{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200108 const char *server_addr; /* address to forward packets to */
109 int server_port; /* port to forward packets to */
110 const char *listen_addr; /* address for accepting client connections */
111 int listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200112
113 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200114 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200115 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200116 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200117 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200118 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200119 int protect_hvr; /* never drop or delay HelloVerifyRequest */
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200120 int protect_len; /* never drop/delay packet of the given size*/
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200121
122 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200123} opt;
124
125static void exit_usage( const char *name, const char *value )
126{
127 if( value == NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200128 printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200129 else
130 printf( " option %s: illegal value: %s\n", name, value );
131
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200132 printf( USAGE );
133 exit( 1 );
134}
135
136static void get_options( int argc, char *argv[] )
137{
138 int i;
139 char *p, *q;
140
141 opt.server_addr = DFL_SERVER_ADDR;
142 opt.server_port = DFL_SERVER_PORT;
143 opt.listen_addr = DFL_LISTEN_ADDR;
144 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200145 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200146
147 for( i = 1; i < argc; i++ )
148 {
149 p = argv[i];
150 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200151 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200152 *q++ = '\0';
153
154 if( strcmp( p, "server_addr" ) == 0 )
155 opt.server_addr = q;
156 else if( strcmp( p, "server_port" ) == 0 )
157 {
158 opt.server_port = atoi( q );
159 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200160 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200161 }
162 else if( strcmp( p, "listen_addr" ) == 0 )
163 opt.listen_addr = q;
164 else if( strcmp( p, "listen_port" ) == 0 )
165 {
166 opt.listen_port = atoi( q );
167 if( opt.listen_port < 1 || opt.listen_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200168 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200169 }
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200170 else if( strcmp( p, "duplicate" ) == 0 )
171 {
172 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200173 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200174 exit_usage( p, q );
175 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200176 else if( strcmp( p, "delay" ) == 0 )
177 {
178 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200179 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200180 exit_usage( p, q );
181 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200182 else if( strcmp( p, "delay_ccs" ) == 0 )
183 {
184 opt.delay_ccs = atoi( q );
185 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
186 exit_usage( p, q );
187 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200188 else if( strcmp( p, "drop" ) == 0 )
189 {
190 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200191 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200192 exit_usage( p, q );
193 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200194 else if( strcmp( p, "mtu" ) == 0 )
195 {
196 opt.mtu = atoi( q );
197 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
198 exit_usage( p, q );
199 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200200 else if( strcmp( p, "bad_ad" ) == 0 )
201 {
202 opt.bad_ad = atoi( q );
203 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
204 exit_usage( p, q );
205 }
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200206 else if( strcmp( p, "protect_hvr" ) == 0 )
207 {
208 opt.protect_hvr = atoi( q );
209 if( opt.protect_hvr < 0 || opt.protect_hvr > 1 )
210 exit_usage( p, q );
211 }
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200212 else if( strcmp( p, "protect_len" ) == 0 )
213 {
214 opt.protect_len = atoi( q );
215 if( opt.protect_len < 0 )
216 exit_usage( p, q );
217 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200218 else if( strcmp( p, "seed" ) == 0 )
219 {
220 opt.seed = atoi( q );
221 if( opt.seed == 0 )
222 exit_usage( p, q );
223 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200224 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200225 exit_usage( p, NULL );
226 }
227}
228
229static const char *msg_type( unsigned char *msg, size_t len )
230{
231 if( len < 1 ) return( "Invalid" );
232 switch( msg[0] )
233 {
234 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
235 case SSL_MSG_ALERT: return( "Alert" );
236 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
237 case SSL_MSG_HANDSHAKE: break; /* See below */
238 default: return( "Unknown" );
239 }
240
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200241 if( len < 13 + 12 ) return( "Invalid handshake" );
242
243 /*
244 * Our handshake message are less than 2^16 bytes long, so they should
245 * have 0 as the first byte of length, frag_offset and frag_length.
246 * Otherwise, assume they are encrypted.
247 */
248 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
249
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200250 switch( msg[13] )
251 {
252 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
253 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
254 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
255 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
256 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
257 case SSL_HS_CERTIFICATE: return( "Certificate" );
258 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
259 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
260 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
261 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
262 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
263 case SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200264 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200265 }
266}
267
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200268/* Return elapsed time in milliseconds since the first call */
269static unsigned long ellapsed_time( void )
270{
271#if defined(_WIN32)
272 return( 0 );
273#else
274 static struct timeval ref = { 0, 0 };
275 struct timeval now;
276
277 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
278 {
279 gettimeofday( &ref, NULL );
280 return( 0 );
281 }
282
283 gettimeofday( &now, NULL );
284 return( 1000 * ( now.tv_sec - ref.tv_sec )
285 + ( now.tv_usec - ref.tv_usec ) / 1000 );
286#endif
287}
288
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200289typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200290{
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200291 int dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200292 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200293 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200294 unsigned len;
295 unsigned char buf[MAX_MSG_SIZE];
296} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200297
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200298/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
299void print_packet( const packet *p, const char *why )
300{
301 if( why == NULL )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200302 printf( " %05lu %s %s (%u bytes)\n",
303 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200304 else
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200305 printf( " %s %s (%u bytes): %s\n",
306 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200307 fflush( stdout );
308}
309
310int send_packet( const packet *p, const char *why )
311{
312 int ret;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200313 int dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200314
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200315 /* insert corrupted ApplicationData record? */
316 if( opt.bad_ad &&
317 strcmp( p->type, "ApplicationData" ) == 0 )
318 {
319 unsigned char buf[MAX_MSG_SIZE];
320 memcpy( buf, p->buf, p->len );
321 ++buf[p->len - 1];
322
323 print_packet( p, "corrupted" );
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200324 if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200325 {
326 printf( " ! net_send returned %d\n", ret );
327 return( ret );
328 }
329 }
330
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200331 print_packet( p, why );
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200332 if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200333 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200334 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200335 return( ret );
336 }
337
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200338 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200339 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200340 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200341 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200342 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200343 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200344
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200345 if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200346 {
347 printf( " ! net_send returned %d\n", ret );
348 return( ret );
349 }
350 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200351
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200352 return( 0 );
353}
354
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200355static packet prev;
356
357void clear_pending( void )
358{
359 memset( &prev, 0, sizeof( packet ) );
360}
361
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200362/*
363 * Avoid dropping or delaying a packet that was already dropped twice: this
364 * only results in uninteresting timeouts. We can't rely on type to identify
365 * packets, since during renegotiation they're all encrypted. So, rely on
366 * size mod 2048 (which is usually just size).
367 */
368static unsigned char dropped[2048] = { 0 };
369#define DROP_MAX 2
370
371/*
372 * OpenSSL groups packets in a datagram the first time it sends them, but not
373 * when it resends them. Count every record as seen the first time.
374 */
375void update_dropped( const packet *p )
376{
377 size_t id = p->len % sizeof( dropped );
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200378 const unsigned char *end = p->buf + p->len;
379 const unsigned char *cur = p->buf;
380 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
381
Manuel Pégourié-Gonnardfa60f122014-09-26 16:07:29 +0200382 ++dropped[id];
383
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200384 /* Avoid counting single record twice */
385 if( len == p->len )
386 return;
387
388 while( cur < end )
389 {
390 size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13;
391
392 id = len % sizeof( dropped );
393 ++dropped[id];
394
395 cur += len;
396 }
397}
398
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200399int handle_message( const char *way, int dst, int src )
400{
401 int ret;
402 packet cur;
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200403 size_t id;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200404
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200405 /* receive packet */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200406 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200407 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200408 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200409 return( ret );
410 }
411
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200412 cur.len = ret;
413 cur.type = msg_type( cur.buf, cur.len );
414 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200415 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200416 print_packet( &cur, NULL );
417
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200418 id = cur.len % sizeof( dropped );
419
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200420 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200421 if( ( opt.mtu != 0 &&
422 cur.len > (unsigned) opt.mtu ) ||
423 ( opt.drop != 0 &&
424 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200425 ! ( opt.protect_hvr &&
426 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200427 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200428 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200429 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200430 {
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200431 update_dropped( &cur );
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200432 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200433 else if( ( opt.delay_ccs == 1 &&
434 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
435 ( opt.delay != 0 &&
436 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnardd0fd1da2014-09-25 17:00:27 +0200437 ! ( opt.protect_hvr &&
438 strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) &&
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200439 prev.dst == 0 &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +0200440 cur.len != (size_t) opt.protect_len &&
Manuel Pégourié-Gonnardae666c52014-09-26 12:08:36 +0200441 dropped[id] < DROP_MAX &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200442 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200443 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200444 memcpy( &prev, &cur, sizeof( packet ) );
445 }
446 else
447 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200448 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200449 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
450 return( ret );
451
452 /* send previously delayed message if any */
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200453 if( prev.dst != 0 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200454 {
455 ret = send_packet( &prev, "delayed" );
456 memset( &prev, 0, sizeof( packet ) );
457 if( ret != 0 )
458 return( ret );
459 }
460 }
461
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200462 return( 0 );
463}
464
465int main( int argc, char *argv[] )
466{
467 int ret;
468
469 int listen_fd = -1;
470 int client_fd = -1;
471 int server_fd = -1;
472
473 int nb_fds;
474 fd_set read_fds;
475
476 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200477
478 /*
479 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
480 * exactly 1 in N packets would lead to problems when a flight has exactly
481 * N packets: the same packet would be dropped on every resend.
482 *
483 * In order to be able to reproduce problems reliably, the seed may be
484 * specified explicitly.
485 */
486 if( opt.seed == 0 )
487 {
488 opt.seed = time( NULL );
489 printf( " . Pseudo-random seed: %u\n", opt.seed );
490 }
491
492 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200493
494 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200495 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200496 */
497 printf( " . Connect to server on UDP/%s/%d ...",
498 opt.server_addr, opt.server_port );
499 fflush( stdout );
500
501 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
502 NET_PROTO_UDP ) ) != 0 )
503 {
504 printf( " failed\n ! net_connect returned %d\n\n", ret );
505 goto exit;
506 }
507
508 printf( " ok\n" );
509
510 /*
511 * 1. Setup the "listening" UDP socket
512 */
513 printf( " . Bind on UDP/%s/%d ...",
514 opt.listen_addr, opt.listen_port );
515 fflush( stdout );
516
517 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
518 NET_PROTO_UDP ) ) != 0 )
519 {
520 printf( " failed\n ! net_bind returned %d\n\n", ret );
521 goto exit;
522 }
523
524 printf( " ok\n" );
525
526 /*
527 * 2. Wait until a client connects
528 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200529accept:
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200530 printf( " . Waiting for a remote connection ..." );
531 fflush( stdout );
532
533 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
534 {
535 printf( " failed\n ! net_accept returned %d\n\n", ret );
536 goto exit;
537 }
538
539 printf( " ok\n" );
540 fflush( stdout );
541
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200542 printf( " . Re-bind on UDP/%s/%d ...",
543 opt.listen_addr, opt.listen_port );
544 fflush( stdout );
545
546 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
547 NET_PROTO_UDP ) ) != 0 )
548 {
549 printf( " failed\n ! net_bind returned %d\n\n", ret );
550 goto exit;
551 }
552
553 printf( " ok\n" );
554
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200555 /*
556 * 3. Forward packets forever (kill the process to terminate it)
557 */
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200558 clear_pending();
559 memset( dropped, 0, sizeof( dropped ) );
560
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200561 nb_fds = client_fd;
562 if( nb_fds < server_fd )
563 nb_fds = server_fd;
564 if( nb_fds < listen_fd )
565 nb_fds = listen_fd;
566 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200567
568 while( 1 )
569 {
570 FD_ZERO( &read_fds );
571 FD_SET( server_fd, &read_fds );
572 FD_SET( client_fd, &read_fds );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200573 FD_SET( listen_fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200574
575 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
576 {
577 perror( "select" );
578 goto exit;
579 }
580
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200581 if( FD_ISSET( listen_fd, &read_fds ) )
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200582 goto accept;
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200583
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200584 if( FD_ISSET( client_fd, &read_fds ) )
585 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200586 if( ( ret = handle_message( "S <- C",
587 server_fd, client_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200588 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200589 }
590
591 if( FD_ISSET( server_fd, &read_fds ) )
592 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200593 if( ( ret = handle_message( "S -> C",
594 client_fd, server_fd ) ) != 0 )
Manuel Pégourié-Gonnardce8588c2014-10-01 00:56:03 +0200595 goto accept;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200596 }
597 }
598
599exit:
600
601#ifdef POLARSSL_ERROR_C
602 if( ret != 0 )
603 {
604 char error_buf[100];
605 polarssl_strerror( ret, error_buf, 100 );
606 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
607 fflush( stdout );
608 }
609#endif
610
611 if( client_fd != -1 )
612 net_close( client_fd );
613
614 if( listen_fd != -1 )
615 net_close( listen_fd );
616
617#if defined(_WIN32)
618 printf( " Press Enter to exit this program.\n" );
619 fflush( stdout ); getchar();
620#endif
621
622 return( ret != 0 );
623}
624
625#endif /* POLARSSL_NET_C */