blob: e82dcb5d5313a7f283599fb92536335a4f68e623 [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é-Gonnardcb4137b2014-09-04 14:55:28 +020088 "\n"
89
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020090/*
91 * global options
92 */
93static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020094{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020095 const char *server_addr; /* address to forward packets to */
96 int server_port; /* port to forward packets to */
97 const char *listen_addr; /* address for accepting client connections */
98 int listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020099
100 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200101 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200102} opt;
103
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200104/*
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200105 * global counters
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200106 */
107static int dupl_cnt;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200108static int delay_cnt;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200109
110/* Do not always start with the same state */
111static void randomize_counters( void )
112{
113#if defined(POLARSSL_HAVE_TIME)
114 srand( time( NULL ) );
115#endif
116
117 if( opt.duplicate != 0 )
118 dupl_cnt = rand() % opt.duplicate;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200119 if( opt.delay != 0 )
120 delay_cnt = rand() % opt.delay;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200121}
122
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200123static void exit_usage( const char *name, const char *value )
124{
125 if( value == NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200126 printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200127 else
128 printf( " option %s: illegal value: %s\n", name, value );
129
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200130 printf( USAGE );
131 exit( 1 );
132}
133
134static void get_options( int argc, char *argv[] )
135{
136 int i;
137 char *p, *q;
138
139 opt.server_addr = DFL_SERVER_ADDR;
140 opt.server_port = DFL_SERVER_PORT;
141 opt.listen_addr = DFL_LISTEN_ADDR;
142 opt.listen_port = DFL_LISTEN_PORT;
143
144 for( i = 1; i < argc; i++ )
145 {
146 p = argv[i];
147 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200148 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200149 *q++ = '\0';
150
151 if( strcmp( p, "server_addr" ) == 0 )
152 opt.server_addr = q;
153 else if( strcmp( p, "server_port" ) == 0 )
154 {
155 opt.server_port = atoi( q );
156 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200157 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200158 }
159 else if( strcmp( p, "listen_addr" ) == 0 )
160 opt.listen_addr = q;
161 else if( strcmp( p, "listen_port" ) == 0 )
162 {
163 opt.listen_port = atoi( q );
164 if( opt.listen_port < 1 || opt.listen_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200165 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200166 }
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200167 else if( strcmp( p, "duplicate" ) == 0 )
168 {
169 opt.duplicate = atoi( q );
170 if( opt.duplicate < 0 || opt.duplicate > 10 )
171 exit_usage( p, q );
172 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200173 else if( strcmp( p, "delay" ) == 0 )
174 {
175 opt.delay = atoi( q );
176 if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
177 exit_usage( p, q );
178 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200179 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200180 exit_usage( p, NULL );
181 }
182}
183
184static const char *msg_type( unsigned char *msg, size_t len )
185{
186 if( len < 1 ) return( "Invalid" );
187 switch( msg[0] )
188 {
189 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
190 case SSL_MSG_ALERT: return( "Alert" );
191 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
192 case SSL_MSG_HANDSHAKE: break; /* See below */
193 default: return( "Unknown" );
194 }
195
196 if( len < 13 ) return( "Invalid handshake" );
197 switch( msg[13] )
198 {
199 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
200 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
201 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
202 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
203 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
204 case SSL_HS_CERTIFICATE: return( "Certificate" );
205 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
206 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
207 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
208 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
209 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
210 case SSL_HS_FINISHED: return( "Finished" );
211 default: return( "Unkown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200212 }
213}
214
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200215typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200216{
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200217 void *dst;
218 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200219 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200220 unsigned len;
221 unsigned char buf[MAX_MSG_SIZE];
222} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200223
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200224/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
225void print_packet( const packet *p, const char *why )
226{
227 if( why == NULL )
228 printf( " > %s: %s (%u bytes)\n", p->way, p->type, p->len );
229 else
230 printf( " < %s: %s (%u bytes): %s\n", p->way, p->type, p->len, why );
231 fflush( stdout );
232}
233
234int send_packet( const packet *p, const char *why )
235{
236 int ret;
237
238 print_packet( p, why );
239 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200240 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200241 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200242 return( ret );
243 }
244
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200245 /* Don't duplicate Application Data, only handshake covered */
246 // Don't duplicate CSS for now (TODO later)
247 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200248 strcmp( p->type, "ApplicationData" ) != 0 &&
249 strcmp( p->type, "ChangeCipherSpec" ) != 0 &&
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200250 ++dupl_cnt == opt.duplicate )
251 {
252 dupl_cnt = 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200253 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200254
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200255 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200256 {
257 printf( " ! net_send returned %d\n", ret );
258 return( ret );
259 }
260 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200261
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200262 return( 0 );
263}
264
265int handle_message( const char *way, int dst, int src )
266{
267 int ret;
268 packet cur;
269 static packet prev;
270
271 /* receivec packet */
272 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200273 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200274 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200275 return( ret );
276 }
277
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200278 cur.len = ret;
279 cur.type = msg_type( cur.buf, cur.len );
280 cur.way = way;
281 cur.dst = &dst;
282 print_packet( &cur, NULL );
283
284 /* do we want to delay it? */
285 if( opt.delay != 0 &&
286 strcmp( cur.type, "ApplicationData" ) != 0 &&
287 ++delay_cnt == opt.delay )
288 {
289 delay_cnt = 0;
290 memcpy( &prev, &cur, sizeof( packet ) );
291 }
292 else
293 {
294 /* not delayed: forward (and possibly duplicate) it */
295 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
296 return( ret );
297
298 /* send previously delayed message if any */
299 if( prev.dst != NULL )
300 {
301 ret = send_packet( &prev, "delayed" );
302 memset( &prev, 0, sizeof( packet ) );
303 if( ret != 0 )
304 return( ret );
305 }
306 }
307
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200308 return( 0 );
309}
310
311int main( int argc, char *argv[] )
312{
313 int ret;
314
315 int listen_fd = -1;
316 int client_fd = -1;
317 int server_fd = -1;
318
319 int nb_fds;
320 fd_set read_fds;
321
322 get_options( argc, argv );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200323 randomize_counters();
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200324
325 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200326 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200327 */
328 printf( " . Connect to server on UDP/%s/%d ...",
329 opt.server_addr, opt.server_port );
330 fflush( stdout );
331
332 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
333 NET_PROTO_UDP ) ) != 0 )
334 {
335 printf( " failed\n ! net_connect returned %d\n\n", ret );
336 goto exit;
337 }
338
339 printf( " ok\n" );
340
341 /*
342 * 1. Setup the "listening" UDP socket
343 */
344 printf( " . Bind on UDP/%s/%d ...",
345 opt.listen_addr, opt.listen_port );
346 fflush( stdout );
347
348 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
349 NET_PROTO_UDP ) ) != 0 )
350 {
351 printf( " failed\n ! net_bind returned %d\n\n", ret );
352 goto exit;
353 }
354
355 printf( " ok\n" );
356
357 /*
358 * 2. Wait until a client connects
359 */
360 printf( " . Waiting for a remote connection ..." );
361 fflush( stdout );
362
363 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
364 {
365 printf( " failed\n ! net_accept returned %d\n\n", ret );
366 goto exit;
367 }
368
369 printf( " ok\n" );
370 fflush( stdout );
371
372 /*
373 * 3. Forward packets forever (kill the process to terminate it)
374 */
375 nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
376
377 while( 1 )
378 {
379 FD_ZERO( &read_fds );
380 FD_SET( server_fd, &read_fds );
381 FD_SET( client_fd, &read_fds );
382
383 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
384 {
385 perror( "select" );
386 goto exit;
387 }
388
389 if( FD_ISSET( client_fd, &read_fds ) )
390 {
391 if( ( ret = handle_message( "c2s", server_fd, client_fd ) ) != 0 )
392 goto exit;
393 }
394
395 if( FD_ISSET( server_fd, &read_fds ) )
396 {
397 if( ( ret = handle_message( "s2c", client_fd, server_fd ) ) != 0 )
398 goto exit;
399 }
400 }
401
402exit:
403
404#ifdef POLARSSL_ERROR_C
405 if( ret != 0 )
406 {
407 char error_buf[100];
408 polarssl_strerror( ret, error_buf, 100 );
409 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
410 fflush( stdout );
411 }
412#endif
413
414 if( client_fd != -1 )
415 net_close( client_fd );
416
417 if( listen_fd != -1 )
418 net_close( listen_fd );
419
420#if defined(_WIN32)
421 printf( " Press Enter to exit this program.\n" );
422 fflush( stdout ); getchar();
423#endif
424
425 return( ret != 0 );
426}
427
428#endif /* POLARSSL_NET_C */