blob: b2b71fa5f155b2f52d9bbadf412a34157757c72a [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é-Gonnard60fdd7e2014-09-06 14:49:52 +020088 " drop=%%d default: 0 (no dropped packets)\n" \
89 " drop 1 packet every N packets\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020090 "\n"
91
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020092/*
93 * global options
94 */
95static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020096{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020097 const char *server_addr; /* address to forward packets to */
98 int server_port; /* port to forward packets to */
99 const char *listen_addr; /* address for accepting client connections */
100 int listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200101
102 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200103 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200104 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200105} opt;
106
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200107/*
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200108 * global counters
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200109 */
110static int dupl_cnt;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200111static int delay_cnt;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200112static int drop_cnt;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200113
114/* Do not always start with the same state */
115static void randomize_counters( void )
116{
117#if defined(POLARSSL_HAVE_TIME)
118 srand( time( NULL ) );
119#endif
120
121 if( opt.duplicate != 0 )
122 dupl_cnt = rand() % opt.duplicate;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200123 if( opt.delay != 0 )
124 delay_cnt = rand() % opt.delay;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200125 if( opt.drop != 0 )
126 drop_cnt = rand() % opt.drop;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200127}
128
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200129static void exit_usage( const char *name, const char *value )
130{
131 if( value == NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200132 printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200133 else
134 printf( " option %s: illegal value: %s\n", name, value );
135
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200136 printf( USAGE );
137 exit( 1 );
138}
139
140static void get_options( int argc, char *argv[] )
141{
142 int i;
143 char *p, *q;
144
145 opt.server_addr = DFL_SERVER_ADDR;
146 opt.server_port = DFL_SERVER_PORT;
147 opt.listen_addr = DFL_LISTEN_ADDR;
148 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200149 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200150
151 for( i = 1; i < argc; i++ )
152 {
153 p = argv[i];
154 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200155 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200156 *q++ = '\0';
157
158 if( strcmp( p, "server_addr" ) == 0 )
159 opt.server_addr = q;
160 else if( strcmp( p, "server_port" ) == 0 )
161 {
162 opt.server_port = atoi( q );
163 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200164 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200165 }
166 else if( strcmp( p, "listen_addr" ) == 0 )
167 opt.listen_addr = q;
168 else if( strcmp( p, "listen_port" ) == 0 )
169 {
170 opt.listen_port = atoi( q );
171 if( opt.listen_port < 1 || opt.listen_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200172 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200173 }
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200174 else if( strcmp( p, "duplicate" ) == 0 )
175 {
176 opt.duplicate = atoi( q );
177 if( opt.duplicate < 0 || opt.duplicate > 10 )
178 exit_usage( p, q );
179 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200180 else if( strcmp( p, "delay" ) == 0 )
181 {
182 opt.delay = atoi( q );
183 if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
184 exit_usage( p, q );
185 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200186 else if( strcmp( p, "drop" ) == 0 )
187 {
188 opt.drop = atoi( q );
189 if( opt.drop < 0 || opt.drop > 10 || opt.drop == 1 )
190 exit_usage( p, q );
191 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200192 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200193 exit_usage( p, NULL );
194 }
195}
196
197static const char *msg_type( unsigned char *msg, size_t len )
198{
199 if( len < 1 ) return( "Invalid" );
200 switch( msg[0] )
201 {
202 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
203 case SSL_MSG_ALERT: return( "Alert" );
204 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
205 case SSL_MSG_HANDSHAKE: break; /* See below */
206 default: return( "Unknown" );
207 }
208
209 if( len < 13 ) return( "Invalid handshake" );
210 switch( msg[13] )
211 {
212 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
213 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
214 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
215 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
216 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
217 case SSL_HS_CERTIFICATE: return( "Certificate" );
218 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
219 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
220 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
221 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
222 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
223 case SSL_HS_FINISHED: return( "Finished" );
224 default: return( "Unkown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200225 }
226}
227
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200228typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200229{
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200230 void *dst;
231 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200232 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200233 unsigned len;
234 unsigned char buf[MAX_MSG_SIZE];
235} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200236
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200237/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
238void print_packet( const packet *p, const char *why )
239{
240 if( why == NULL )
241 printf( " > %s: %s (%u bytes)\n", p->way, p->type, p->len );
242 else
243 printf( " < %s: %s (%u bytes): %s\n", p->way, p->type, p->len, why );
244 fflush( stdout );
245}
246
247int send_packet( const packet *p, const char *why )
248{
249 int ret;
250
251 print_packet( p, why );
252 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200253 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200254 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200255 return( ret );
256 }
257
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200258 /* Don't duplicate Application Data, only handshake covered */
259 // Don't duplicate CSS for now (TODO later)
260 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200261 strcmp( p->type, "ApplicationData" ) != 0 &&
262 strcmp( p->type, "ChangeCipherSpec" ) != 0 &&
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200263 ++dupl_cnt == opt.duplicate )
264 {
265 dupl_cnt = 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200266 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200267
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200268 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200269 {
270 printf( " ! net_send returned %d\n", ret );
271 return( ret );
272 }
273 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200274
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200275 return( 0 );
276}
277
278int handle_message( const char *way, int dst, int src )
279{
280 int ret;
281 packet cur;
282 static packet prev;
283
284 /* receivec packet */
285 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200286 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200287 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200288 return( ret );
289 }
290
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200291 cur.len = ret;
292 cur.type = msg_type( cur.buf, cur.len );
293 cur.way = way;
294 cur.dst = &dst;
295 print_packet( &cur, NULL );
296
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200297 /* do we want to drop, delay, or forward it? */
298 if( opt.drop != 0 &&
299 strcmp( cur.type, "ApplicationData" ) != 0 &&
300 ++drop_cnt == opt.drop )
301 {
302 drop_cnt = 0;
303 }
304 else if( opt.delay != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200305 strcmp( cur.type, "ApplicationData" ) != 0 &&
306 ++delay_cnt == opt.delay )
307 {
308 delay_cnt = 0;
309 memcpy( &prev, &cur, sizeof( packet ) );
310 }
311 else
312 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200313 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200314 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
315 return( ret );
316
317 /* send previously delayed message if any */
318 if( prev.dst != NULL )
319 {
320 ret = send_packet( &prev, "delayed" );
321 memset( &prev, 0, sizeof( packet ) );
322 if( ret != 0 )
323 return( ret );
324 }
325 }
326
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200327 return( 0 );
328}
329
330int main( int argc, char *argv[] )
331{
332 int ret;
333
334 int listen_fd = -1;
335 int client_fd = -1;
336 int server_fd = -1;
337
338 int nb_fds;
339 fd_set read_fds;
340
341 get_options( argc, argv );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200342 randomize_counters();
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200343
344 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200345 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200346 */
347 printf( " . Connect to server on UDP/%s/%d ...",
348 opt.server_addr, opt.server_port );
349 fflush( stdout );
350
351 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
352 NET_PROTO_UDP ) ) != 0 )
353 {
354 printf( " failed\n ! net_connect returned %d\n\n", ret );
355 goto exit;
356 }
357
358 printf( " ok\n" );
359
360 /*
361 * 1. Setup the "listening" UDP socket
362 */
363 printf( " . Bind on UDP/%s/%d ...",
364 opt.listen_addr, opt.listen_port );
365 fflush( stdout );
366
367 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
368 NET_PROTO_UDP ) ) != 0 )
369 {
370 printf( " failed\n ! net_bind returned %d\n\n", ret );
371 goto exit;
372 }
373
374 printf( " ok\n" );
375
376 /*
377 * 2. Wait until a client connects
378 */
379 printf( " . Waiting for a remote connection ..." );
380 fflush( stdout );
381
382 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
383 {
384 printf( " failed\n ! net_accept returned %d\n\n", ret );
385 goto exit;
386 }
387
388 printf( " ok\n" );
389 fflush( stdout );
390
391 /*
392 * 3. Forward packets forever (kill the process to terminate it)
393 */
394 nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
395
396 while( 1 )
397 {
398 FD_ZERO( &read_fds );
399 FD_SET( server_fd, &read_fds );
400 FD_SET( client_fd, &read_fds );
401
402 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
403 {
404 perror( "select" );
405 goto exit;
406 }
407
408 if( FD_ISSET( client_fd, &read_fds ) )
409 {
410 if( ( ret = handle_message( "c2s", server_fd, client_fd ) ) != 0 )
411 goto exit;
412 }
413
414 if( FD_ISSET( server_fd, &read_fds ) )
415 {
416 if( ( ret = handle_message( "s2c", client_fd, server_fd ) ) != 0 )
417 goto exit;
418 }
419 }
420
421exit:
422
423#ifdef POLARSSL_ERROR_C
424 if( ret != 0 )
425 {
426 char error_buf[100];
427 polarssl_strerror( ret, error_buf, 100 );
428 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
429 fflush( stdout );
430 }
431#endif
432
433 if( client_fd != -1 )
434 net_close( client_fd );
435
436 if( listen_fd != -1 )
437 net_close( listen_fd );
438
439#if defined(_WIN32)
440 printf( " Press Enter to exit this program.\n" );
441 fflush( stdout ); getchar();
442#endif
443
444 return( ret != 0 );
445}
446
447#endif /* POLARSSL_NET_C */