blob: 0ba9216eb080c2103cd8423338713ed397d32b07 [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#if defined(POLARSSL_HAVE_TIME)
48#include <time.h>
49#endif
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020050
51/* For select() */
52#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
53 !defined(EFI32)
54#include <winsock2.h>
55#include <windows.h>
56#if defined(_MSC_VER)
57#if defined(_WIN32_WCE)
58#pragma comment( lib, "ws2.lib" )
59#else
60#pragma comment( lib, "ws2_32.lib" )
61#endif
62#endif /* _MSC_VER */
63#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
64#include <sys/time.h>
65#include <sys/types.h>
66#include <unistd.h>
67#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
68
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020069#define MAX_MSG_SIZE 4096 /* Reasonable max size for our tests */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020070
71#define DFL_SERVER_ADDR "localhost"
72#define DFL_SERVER_PORT 4433
73#define DFL_LISTEN_ADDR "localhost"
74#define DFL_LISTEN_PORT 5556
75
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020076#define USAGE \
77 "\n usage: udp_proxy param=<>...\n" \
78 "\n acceptable parameters:\n" \
79 " server_addr=%%d default: localhost\n" \
80 " server_port=%%d default: 4433\n" \
81 " listen_addr=%%d default: localhost\n" \
82 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020083 "\n" \
84 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020085 " duplicate 1 packet every N packets\n" \
86 " delay=%%d default: 0 (no delayed packets)\n" \
87 " delay 1 packet every N packets\n" \
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +020088 " delay_ccs=%%d default: 0 (don't delay ChangeCipherSuite)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +020089 " drop=%%d default: 0 (no dropped packets)\n" \
90 " drop 1 packet every N packets\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +020091 " mtu=%%d default: 0 (unlimited)\n" \
92 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020093 "\n"
94
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020095/*
96 * global options
97 */
98static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200100 const char *server_addr; /* address to forward packets to */
101 int server_port; /* port to forward packets to */
102 const char *listen_addr; /* address for accepting client connections */
103 int listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200104
105 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200106 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200107 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200108 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200109 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200110} opt;
111
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200112/*
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200113 * global counters
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200114 */
115static int dupl_cnt;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200116static int delay_cnt;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200117static int drop_cnt;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200118
119/* Do not always start with the same state */
120static void randomize_counters( void )
121{
122#if defined(POLARSSL_HAVE_TIME)
123 srand( time( NULL ) );
124#endif
125
126 if( opt.duplicate != 0 )
127 dupl_cnt = rand() % opt.duplicate;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200128 if( opt.delay != 0 )
129 delay_cnt = rand() % opt.delay;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200130 if( opt.drop != 0 )
131 drop_cnt = rand() % opt.drop;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200132}
133
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200134static void exit_usage( const char *name, const char *value )
135{
136 if( value == NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200137 printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200138 else
139 printf( " option %s: illegal value: %s\n", name, value );
140
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200141 printf( USAGE );
142 exit( 1 );
143}
144
145static void get_options( int argc, char *argv[] )
146{
147 int i;
148 char *p, *q;
149
150 opt.server_addr = DFL_SERVER_ADDR;
151 opt.server_port = DFL_SERVER_PORT;
152 opt.listen_addr = DFL_LISTEN_ADDR;
153 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200154 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200155
156 for( i = 1; i < argc; i++ )
157 {
158 p = argv[i];
159 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200160 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200161 *q++ = '\0';
162
163 if( strcmp( p, "server_addr" ) == 0 )
164 opt.server_addr = q;
165 else if( strcmp( p, "server_port" ) == 0 )
166 {
167 opt.server_port = atoi( q );
168 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200169 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200170 }
171 else if( strcmp( p, "listen_addr" ) == 0 )
172 opt.listen_addr = q;
173 else if( strcmp( p, "listen_port" ) == 0 )
174 {
175 opt.listen_port = atoi( q );
176 if( opt.listen_port < 1 || opt.listen_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200177 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200178 }
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200179 else if( strcmp( p, "duplicate" ) == 0 )
180 {
181 opt.duplicate = atoi( q );
182 if( opt.duplicate < 0 || opt.duplicate > 10 )
183 exit_usage( p, q );
184 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200185 else if( strcmp( p, "delay" ) == 0 )
186 {
187 opt.delay = atoi( q );
188 if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
189 exit_usage( p, q );
190 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200191 else if( strcmp( p, "delay_ccs" ) == 0 )
192 {
193 opt.delay_ccs = atoi( q );
194 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
195 exit_usage( p, q );
196 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200197 else if( strcmp( p, "drop" ) == 0 )
198 {
199 opt.drop = atoi( q );
200 if( opt.drop < 0 || opt.drop > 10 || opt.drop == 1 )
201 exit_usage( p, q );
202 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200203 else if( strcmp( p, "mtu" ) == 0 )
204 {
205 opt.mtu = atoi( q );
206 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
207 exit_usage( p, q );
208 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200209 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200210 exit_usage( p, NULL );
211 }
212}
213
214static const char *msg_type( unsigned char *msg, size_t len )
215{
216 if( len < 1 ) return( "Invalid" );
217 switch( msg[0] )
218 {
219 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
220 case SSL_MSG_ALERT: return( "Alert" );
221 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
222 case SSL_MSG_HANDSHAKE: break; /* See below */
223 default: return( "Unknown" );
224 }
225
226 if( len < 13 ) return( "Invalid handshake" );
227 switch( msg[13] )
228 {
229 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
230 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
231 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
232 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
233 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
234 case SSL_HS_CERTIFICATE: return( "Certificate" );
235 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
236 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
237 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
238 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
239 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
240 case SSL_HS_FINISHED: return( "Finished" );
241 default: return( "Unkown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200242 }
243}
244
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200245typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200246{
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200247 void *dst;
248 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200249 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200250 unsigned len;
251 unsigned char buf[MAX_MSG_SIZE];
252} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200253
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200254/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
255void print_packet( const packet *p, const char *why )
256{
257 if( why == NULL )
258 printf( " > %s: %s (%u bytes)\n", p->way, p->type, p->len );
259 else
260 printf( " < %s: %s (%u bytes): %s\n", p->way, p->type, p->len, why );
261 fflush( stdout );
262}
263
264int send_packet( const packet *p, const char *why )
265{
266 int ret;
267
268 print_packet( p, why );
269 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200270 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200271 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200272 return( ret );
273 }
274
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200275 /* Don't duplicate Application Data, only handshake covered */
276 // Don't duplicate CSS for now (TODO later)
277 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200278 strcmp( p->type, "ApplicationData" ) != 0 &&
279 strcmp( p->type, "ChangeCipherSpec" ) != 0 &&
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200280 ++dupl_cnt == opt.duplicate )
281 {
282 dupl_cnt = 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200283 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200284
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200285 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200286 {
287 printf( " ! net_send returned %d\n", ret );
288 return( ret );
289 }
290 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200291
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200292 return( 0 );
293}
294
295int handle_message( const char *way, int dst, int src )
296{
297 int ret;
298 packet cur;
299 static packet prev;
300
301 /* receivec packet */
302 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200303 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200304 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200305 return( ret );
306 }
307
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200308 cur.len = ret;
309 cur.type = msg_type( cur.buf, cur.len );
310 cur.way = way;
311 cur.dst = &dst;
312 print_packet( &cur, NULL );
313
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200314 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200315 if( ( opt.mtu != 0 &&
316 cur.len > (unsigned) opt.mtu ) ||
317 ( opt.drop != 0 &&
318 strcmp( cur.type, "ApplicationData" ) != 0 &&
319 ++drop_cnt == opt.drop ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200320 {
321 drop_cnt = 0;
322 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200323 else if( ( opt.delay_ccs == 1 &&
324 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
325 ( opt.delay != 0 &&
326 strcmp( cur.type, "ApplicationData" ) != 0 &&
327 ++delay_cnt == opt.delay ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200328 {
329 delay_cnt = 0;
330 memcpy( &prev, &cur, sizeof( packet ) );
331 }
332 else
333 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200334 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200335 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
336 return( ret );
337
338 /* send previously delayed message if any */
339 if( prev.dst != NULL )
340 {
341 ret = send_packet( &prev, "delayed" );
342 memset( &prev, 0, sizeof( packet ) );
343 if( ret != 0 )
344 return( ret );
345 }
346 }
347
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200348 return( 0 );
349}
350
351int main( int argc, char *argv[] )
352{
353 int ret;
354
355 int listen_fd = -1;
356 int client_fd = -1;
357 int server_fd = -1;
358
359 int nb_fds;
360 fd_set read_fds;
361
362 get_options( argc, argv );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200363 randomize_counters();
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200364
365 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200366 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200367 */
368 printf( " . Connect to server on UDP/%s/%d ...",
369 opt.server_addr, opt.server_port );
370 fflush( stdout );
371
372 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
373 NET_PROTO_UDP ) ) != 0 )
374 {
375 printf( " failed\n ! net_connect returned %d\n\n", ret );
376 goto exit;
377 }
378
379 printf( " ok\n" );
380
381 /*
382 * 1. Setup the "listening" UDP socket
383 */
384 printf( " . Bind on UDP/%s/%d ...",
385 opt.listen_addr, opt.listen_port );
386 fflush( stdout );
387
388 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
389 NET_PROTO_UDP ) ) != 0 )
390 {
391 printf( " failed\n ! net_bind returned %d\n\n", ret );
392 goto exit;
393 }
394
395 printf( " ok\n" );
396
397 /*
398 * 2. Wait until a client connects
399 */
400 printf( " . Waiting for a remote connection ..." );
401 fflush( stdout );
402
403 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
404 {
405 printf( " failed\n ! net_accept returned %d\n\n", ret );
406 goto exit;
407 }
408
409 printf( " ok\n" );
410 fflush( stdout );
411
412 /*
413 * 3. Forward packets forever (kill the process to terminate it)
414 */
415 nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
416
417 while( 1 )
418 {
419 FD_ZERO( &read_fds );
420 FD_SET( server_fd, &read_fds );
421 FD_SET( client_fd, &read_fds );
422
423 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
424 {
425 perror( "select" );
426 goto exit;
427 }
428
429 if( FD_ISSET( client_fd, &read_fds ) )
430 {
431 if( ( ret = handle_message( "c2s", server_fd, client_fd ) ) != 0 )
432 goto exit;
433 }
434
435 if( FD_ISSET( server_fd, &read_fds ) )
436 {
437 if( ( ret = handle_message( "s2c", client_fd, server_fd ) ) != 0 )
438 goto exit;
439 }
440 }
441
442exit:
443
444#ifdef POLARSSL_ERROR_C
445 if( ret != 0 )
446 {
447 char error_buf[100];
448 polarssl_strerror( ret, error_buf, 100 );
449 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
450 fflush( stdout );
451 }
452#endif
453
454 if( client_fd != -1 )
455 net_close( client_fd );
456
457 if( listen_fd != -1 )
458 net_close( listen_fd );
459
460#if defined(_WIN32)
461 printf( " Press Enter to exit this program.\n" );
462 fflush( stdout ); getchar();
463#endif
464
465 return( ret != 0 );
466}
467
468#endif /* POLARSSL_NET_C */