blob: ad05b48802a10fdbdbe34e682d3256be4c5c70f9 [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" \
82 " server_addr=%%d default: localhost\n" \
83 " server_port=%%d default: 4433\n" \
84 " listen_addr=%%d default: localhost\n" \
85 " 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é-Gonnardae8d2392014-09-23 10:01:06 +020091 " delay_ccs=%%d 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é-Gonnard6c18a392014-09-08 11:24:58 +020096 " bad_ad=%%d default: 0 (don't add bad ApplicationData)\n" \
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +020097 "\n" \
98 " seed=%%d default: (use current time)\n" \
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099 "\n"
100
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200101/*
102 * global options
103 */
104static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200105{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200106 const char *server_addr; /* address to forward packets to */
107 int server_port; /* port to forward packets to */
108 const char *listen_addr; /* address for accepting client connections */
109 int listen_port; /* port for accepting client connections */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200110
111 int duplicate; /* duplicate 1 in N packets (none if 0) */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200112 int delay; /* delay 1 packet in N (none if 0) */
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200113 int delay_ccs; /* delay ChangeCipherSpec */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200114 int drop; /* drop 1 packet in N (none if 0) */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200115 int mtu; /* drop packets larger than this */
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200116 int bad_ad; /* inject corrupted ApplicationData record */
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200117
118 unsigned int seed; /* seed for "random" events */
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200119} opt;
120
121static void exit_usage( const char *name, const char *value )
122{
123 if( value == NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200124 printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200125 else
126 printf( " option %s: illegal value: %s\n", name, value );
127
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200128 printf( USAGE );
129 exit( 1 );
130}
131
132static void get_options( int argc, char *argv[] )
133{
134 int i;
135 char *p, *q;
136
137 opt.server_addr = DFL_SERVER_ADDR;
138 opt.server_port = DFL_SERVER_PORT;
139 opt.listen_addr = DFL_LISTEN_ADDR;
140 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200141 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200142
143 for( i = 1; i < argc; i++ )
144 {
145 p = argv[i];
146 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200147 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200148 *q++ = '\0';
149
150 if( strcmp( p, "server_addr" ) == 0 )
151 opt.server_addr = q;
152 else if( strcmp( p, "server_port" ) == 0 )
153 {
154 opt.server_port = atoi( q );
155 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200156 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200157 }
158 else if( strcmp( p, "listen_addr" ) == 0 )
159 opt.listen_addr = q;
160 else if( strcmp( p, "listen_port" ) == 0 )
161 {
162 opt.listen_port = atoi( q );
163 if( opt.listen_port < 1 || opt.listen_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 }
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200166 else if( strcmp( p, "duplicate" ) == 0 )
167 {
168 opt.duplicate = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200169 if( opt.duplicate < 0 || opt.duplicate > 20 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200170 exit_usage( p, q );
171 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200172 else if( strcmp( p, "delay" ) == 0 )
173 {
174 opt.delay = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200175 if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200176 exit_usage( p, q );
177 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200178 else if( strcmp( p, "delay_ccs" ) == 0 )
179 {
180 opt.delay_ccs = atoi( q );
181 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
182 exit_usage( p, q );
183 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200184 else if( strcmp( p, "drop" ) == 0 )
185 {
186 opt.drop = atoi( q );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200187 if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200188 exit_usage( p, q );
189 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200190 else if( strcmp( p, "mtu" ) == 0 )
191 {
192 opt.mtu = atoi( q );
193 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
194 exit_usage( p, q );
195 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200196 else if( strcmp( p, "bad_ad" ) == 0 )
197 {
198 opt.bad_ad = atoi( q );
199 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
200 exit_usage( p, q );
201 }
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200202 else if( strcmp( p, "seed" ) == 0 )
203 {
204 opt.seed = atoi( q );
205 if( opt.seed == 0 )
206 exit_usage( p, q );
207 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200208 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200209 exit_usage( p, NULL );
210 }
211}
212
213static const char *msg_type( unsigned char *msg, size_t len )
214{
215 if( len < 1 ) return( "Invalid" );
216 switch( msg[0] )
217 {
218 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
219 case SSL_MSG_ALERT: return( "Alert" );
220 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
221 case SSL_MSG_HANDSHAKE: break; /* See below */
222 default: return( "Unknown" );
223 }
224
Manuel Pégourié-Gonnard8cc7e032014-09-25 12:59:05 +0200225 if( len < 13 + 12 ) return( "Invalid handshake" );
226
227 /*
228 * Our handshake message are less than 2^16 bytes long, so they should
229 * have 0 as the first byte of length, frag_offset and frag_length.
230 * Otherwise, assume they are encrypted.
231 */
232 if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" );
233
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200234 switch( msg[13] )
235 {
236 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
237 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
238 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
239 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
240 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
241 case SSL_HS_CERTIFICATE: return( "Certificate" );
242 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
243 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
244 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
245 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
246 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
247 case SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200248 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200249 }
250}
251
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200252/* Return elapsed time in milliseconds since the first call */
253static unsigned long ellapsed_time( void )
254{
255#if defined(_WIN32)
256 return( 0 );
257#else
258 static struct timeval ref = { 0, 0 };
259 struct timeval now;
260
261 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
262 {
263 gettimeofday( &ref, NULL );
264 return( 0 );
265 }
266
267 gettimeofday( &now, NULL );
268 return( 1000 * ( now.tv_sec - ref.tv_sec )
269 + ( now.tv_usec - ref.tv_usec ) / 1000 );
270#endif
271}
272
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200273typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200274{
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200275 int dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200276 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200277 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200278 unsigned len;
279 unsigned char buf[MAX_MSG_SIZE];
280} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200281
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200282/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
283void print_packet( const packet *p, const char *why )
284{
285 if( why == NULL )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200286 printf( " %05lu %s %s (%u bytes)\n",
287 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200288 else
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200289 printf( " %s %s (%u bytes): %s\n",
290 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200291 fflush( stdout );
292}
293
294int send_packet( const packet *p, const char *why )
295{
296 int ret;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200297 int dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200298
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200299 /* insert corrupted ApplicationData record? */
300 if( opt.bad_ad &&
301 strcmp( p->type, "ApplicationData" ) == 0 )
302 {
303 unsigned char buf[MAX_MSG_SIZE];
304 memcpy( buf, p->buf, p->len );
305 ++buf[p->len - 1];
306
307 print_packet( p, "corrupted" );
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200308 if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200309 {
310 printf( " ! net_send returned %d\n", ret );
311 return( ret );
312 }
313 }
314
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200315 print_packet( p, why );
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200316 if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200317 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200318 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200319 return( ret );
320 }
321
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200322 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200323 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200324 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200325 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200326 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200327 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200328
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200329 if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200330 {
331 printf( " ! net_send returned %d\n", ret );
332 return( ret );
333 }
334 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200335
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200336 return( 0 );
337}
338
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200339static packet prev;
340
341void clear_pending( void )
342{
343 memset( &prev, 0, sizeof( packet ) );
344}
345
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200346int handle_message( const char *way, int dst, int src )
347{
348 int ret;
349 packet cur;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200350
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200351 /* receive packet */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200352 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200353 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200354 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200355 return( ret );
356 }
357
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200358 cur.len = ret;
359 cur.type = msg_type( cur.buf, cur.len );
360 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200361 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200362 print_packet( &cur, NULL );
363
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200364 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200365 if( ( opt.mtu != 0 &&
366 cur.len > (unsigned) opt.mtu ) ||
367 ( opt.drop != 0 &&
368 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200369 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200370 {
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200371 ; /* Nothing to do */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200372 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200373 else if( ( opt.delay_ccs == 1 &&
374 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
375 ( opt.delay != 0 &&
376 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200377 prev.dst == 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200378 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200379 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200380 memcpy( &prev, &cur, sizeof( packet ) );
381 }
382 else
383 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200384 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200385 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
386 return( ret );
387
388 /* send previously delayed message if any */
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200389 if( prev.dst != 0 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200390 {
391 ret = send_packet( &prev, "delayed" );
392 memset( &prev, 0, sizeof( packet ) );
393 if( ret != 0 )
394 return( ret );
395 }
396 }
397
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200398 return( 0 );
399}
400
401int main( int argc, char *argv[] )
402{
403 int ret;
404
405 int listen_fd = -1;
406 int client_fd = -1;
407 int server_fd = -1;
408
409 int nb_fds;
410 fd_set read_fds;
411
412 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200413
414 /*
415 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
416 * exactly 1 in N packets would lead to problems when a flight has exactly
417 * N packets: the same packet would be dropped on every resend.
418 *
419 * In order to be able to reproduce problems reliably, the seed may be
420 * specified explicitly.
421 */
422 if( opt.seed == 0 )
423 {
424 opt.seed = time( NULL );
425 printf( " . Pseudo-random seed: %u\n", opt.seed );
426 }
427
428 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200429
430 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200431 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200432 */
433 printf( " . Connect to server on UDP/%s/%d ...",
434 opt.server_addr, opt.server_port );
435 fflush( stdout );
436
437 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
438 NET_PROTO_UDP ) ) != 0 )
439 {
440 printf( " failed\n ! net_connect returned %d\n\n", ret );
441 goto exit;
442 }
443
444 printf( " ok\n" );
445
446 /*
447 * 1. Setup the "listening" UDP socket
448 */
449 printf( " . Bind on UDP/%s/%d ...",
450 opt.listen_addr, opt.listen_port );
451 fflush( stdout );
452
453 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
454 NET_PROTO_UDP ) ) != 0 )
455 {
456 printf( " failed\n ! net_bind returned %d\n\n", ret );
457 goto exit;
458 }
459
460 printf( " ok\n" );
461
462 /*
463 * 2. Wait until a client connects
464 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200465accept:
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200466 printf( " . Waiting for a remote connection ..." );
467 fflush( stdout );
468
469 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
470 {
471 printf( " failed\n ! net_accept returned %d\n\n", ret );
472 goto exit;
473 }
474
475 printf( " ok\n" );
476 fflush( stdout );
477
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200478 printf( " . Re-bind on UDP/%s/%d ...",
479 opt.listen_addr, opt.listen_port );
480 fflush( stdout );
481
482 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
483 NET_PROTO_UDP ) ) != 0 )
484 {
485 printf( " failed\n ! net_bind returned %d\n\n", ret );
486 goto exit;
487 }
488
489 printf( " ok\n" );
490
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200491 /*
492 * 3. Forward packets forever (kill the process to terminate it)
493 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200494 nb_fds = client_fd;
495 if( nb_fds < server_fd )
496 nb_fds = server_fd;
497 if( nb_fds < listen_fd )
498 nb_fds = listen_fd;
499 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200500
501 while( 1 )
502 {
503 FD_ZERO( &read_fds );
504 FD_SET( server_fd, &read_fds );
505 FD_SET( client_fd, &read_fds );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200506 FD_SET( listen_fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200507
508 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
509 {
510 perror( "select" );
511 goto exit;
512 }
513
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200514 if( FD_ISSET( listen_fd, &read_fds ) )
515 {
516 clear_pending();
517 goto accept;
518 }
519
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200520 if( FD_ISSET( client_fd, &read_fds ) )
521 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200522 if( ( ret = handle_message( "S <- C",
523 server_fd, client_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200524 goto exit;
525 }
526
527 if( FD_ISSET( server_fd, &read_fds ) )
528 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200529 if( ( ret = handle_message( "S -> C",
530 client_fd, server_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200531 goto exit;
532 }
533 }
534
535exit:
536
537#ifdef POLARSSL_ERROR_C
538 if( ret != 0 )
539 {
540 char error_buf[100];
541 polarssl_strerror( ret, error_buf, 100 );
542 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
543 fflush( stdout );
544 }
545#endif
546
547 if( client_fd != -1 )
548 net_close( client_fd );
549
550 if( listen_fd != -1 )
551 net_close( listen_fd );
552
553#if defined(_WIN32)
554 printf( " Press Enter to exit this program.\n" );
555 fflush( stdout ); getchar();
556#endif
557
558 return( ret != 0 );
559}
560
561#endif /* POLARSSL_NET_C */