blob: 89dfcee1b3e570ffcfb4b36293549d1d40aef26a [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
225 if( len < 13 ) return( "Invalid handshake" );
226 switch( msg[13] )
227 {
228 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
229 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
230 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
231 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
232 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
233 case SSL_HS_CERTIFICATE: return( "Certificate" );
234 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
235 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
236 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
237 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
238 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
239 case SSL_HS_FINISHED: return( "Finished" );
Manuel Pégourié-Gonnardbc010a02014-09-20 12:40:51 +0200240 default: return( "Unknown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200241 }
242}
243
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200244/* Return elapsed time in milliseconds since the first call */
245static unsigned long ellapsed_time( void )
246{
247#if defined(_WIN32)
248 return( 0 );
249#else
250 static struct timeval ref = { 0, 0 };
251 struct timeval now;
252
253 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
254 {
255 gettimeofday( &ref, NULL );
256 return( 0 );
257 }
258
259 gettimeofday( &now, NULL );
260 return( 1000 * ( now.tv_sec - ref.tv_sec )
261 + ( now.tv_usec - ref.tv_usec ) / 1000 );
262#endif
263}
264
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200265typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200266{
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200267 int dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200268 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200269 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200270 unsigned len;
271 unsigned char buf[MAX_MSG_SIZE];
272} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200273
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200274/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
275void print_packet( const packet *p, const char *why )
276{
277 if( why == NULL )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200278 printf( " %05lu %s %s (%u bytes)\n",
279 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200280 else
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200281 printf( " %s %s (%u bytes): %s\n",
282 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200283 fflush( stdout );
284}
285
286int send_packet( const packet *p, const char *why )
287{
288 int ret;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200289 int dst = p->dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200290
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200291 /* insert corrupted ApplicationData record? */
292 if( opt.bad_ad &&
293 strcmp( p->type, "ApplicationData" ) == 0 )
294 {
295 unsigned char buf[MAX_MSG_SIZE];
296 memcpy( buf, p->buf, p->len );
297 ++buf[p->len - 1];
298
299 print_packet( p, "corrupted" );
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200300 if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200301 {
302 printf( " ! net_send returned %d\n", ret );
303 return( ret );
304 }
305 }
306
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200307 print_packet( p, why );
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200308 if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200309 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200310 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200311 return( ret );
312 }
313
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200314 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200315 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200316 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200317 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200318 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200319 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200320
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200321 if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200322 {
323 printf( " ! net_send returned %d\n", ret );
324 return( ret );
325 }
326 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200327
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200328 return( 0 );
329}
330
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200331static packet prev;
332
333void clear_pending( void )
334{
335 memset( &prev, 0, sizeof( packet ) );
336}
337
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200338int handle_message( const char *way, int dst, int src )
339{
340 int ret;
341 packet cur;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200342
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200343 /* receive packet */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200344 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200345 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200346 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200347 return( ret );
348 }
349
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200350 cur.len = ret;
351 cur.type = msg_type( cur.buf, cur.len );
352 cur.way = way;
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200353 cur.dst = dst;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200354 print_packet( &cur, NULL );
355
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200356 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200357 if( ( opt.mtu != 0 &&
358 cur.len > (unsigned) opt.mtu ) ||
359 ( opt.drop != 0 &&
360 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200361 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200362 {
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200363 ; /* Nothing to do */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200364 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200365 else if( ( opt.delay_ccs == 1 &&
366 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
367 ( opt.delay != 0 &&
368 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200369 prev.dst == 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200370 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200371 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200372 memcpy( &prev, &cur, sizeof( packet ) );
373 }
374 else
375 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200376 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200377 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
378 return( ret );
379
380 /* send previously delayed message if any */
Manuel Pégourié-Gonnard6265d302014-09-24 17:42:09 +0200381 if( prev.dst != 0 )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200382 {
383 ret = send_packet( &prev, "delayed" );
384 memset( &prev, 0, sizeof( packet ) );
385 if( ret != 0 )
386 return( ret );
387 }
388 }
389
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200390 return( 0 );
391}
392
393int main( int argc, char *argv[] )
394{
395 int ret;
396
397 int listen_fd = -1;
398 int client_fd = -1;
399 int server_fd = -1;
400
401 int nb_fds;
402 fd_set read_fds;
403
404 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200405
406 /*
407 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
408 * exactly 1 in N packets would lead to problems when a flight has exactly
409 * N packets: the same packet would be dropped on every resend.
410 *
411 * In order to be able to reproduce problems reliably, the seed may be
412 * specified explicitly.
413 */
414 if( opt.seed == 0 )
415 {
416 opt.seed = time( NULL );
417 printf( " . Pseudo-random seed: %u\n", opt.seed );
418 }
419
420 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200421
422 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200423 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200424 */
425 printf( " . Connect to server on UDP/%s/%d ...",
426 opt.server_addr, opt.server_port );
427 fflush( stdout );
428
429 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
430 NET_PROTO_UDP ) ) != 0 )
431 {
432 printf( " failed\n ! net_connect returned %d\n\n", ret );
433 goto exit;
434 }
435
436 printf( " ok\n" );
437
438 /*
439 * 1. Setup the "listening" UDP socket
440 */
441 printf( " . Bind on UDP/%s/%d ...",
442 opt.listen_addr, opt.listen_port );
443 fflush( stdout );
444
445 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
446 NET_PROTO_UDP ) ) != 0 )
447 {
448 printf( " failed\n ! net_bind returned %d\n\n", ret );
449 goto exit;
450 }
451
452 printf( " ok\n" );
453
454 /*
455 * 2. Wait until a client connects
456 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200457accept:
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200458 printf( " . Waiting for a remote connection ..." );
459 fflush( stdout );
460
461 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
462 {
463 printf( " failed\n ! net_accept returned %d\n\n", ret );
464 goto exit;
465 }
466
467 printf( " ok\n" );
468 fflush( stdout );
469
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200470 printf( " . Re-bind on UDP/%s/%d ...",
471 opt.listen_addr, opt.listen_port );
472 fflush( stdout );
473
474 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
475 NET_PROTO_UDP ) ) != 0 )
476 {
477 printf( " failed\n ! net_bind returned %d\n\n", ret );
478 goto exit;
479 }
480
481 printf( " ok\n" );
482
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200483 /*
484 * 3. Forward packets forever (kill the process to terminate it)
485 */
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200486 nb_fds = client_fd;
487 if( nb_fds < server_fd )
488 nb_fds = server_fd;
489 if( nb_fds < listen_fd )
490 nb_fds = listen_fd;
491 ++nb_fds;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200492
493 while( 1 )
494 {
495 FD_ZERO( &read_fds );
496 FD_SET( server_fd, &read_fds );
497 FD_SET( client_fd, &read_fds );
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200498 FD_SET( listen_fd, &read_fds );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200499
500 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
501 {
502 perror( "select" );
503 goto exit;
504 }
505
Manuel Pégourié-Gonnard6312e0f2014-09-23 12:46:33 +0200506 if( FD_ISSET( listen_fd, &read_fds ) )
507 {
508 clear_pending();
509 goto accept;
510 }
511
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200512 if( FD_ISSET( client_fd, &read_fds ) )
513 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200514 if( ( ret = handle_message( "S <- C",
515 server_fd, client_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200516 goto exit;
517 }
518
519 if( FD_ISSET( server_fd, &read_fds ) )
520 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200521 if( ( ret = handle_message( "S -> C",
522 client_fd, server_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200523 goto exit;
524 }
525 }
526
527exit:
528
529#ifdef POLARSSL_ERROR_C
530 if( ret != 0 )
531 {
532 char error_buf[100];
533 polarssl_strerror( ret, error_buf, 100 );
534 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
535 fflush( stdout );
536 }
537#endif
538
539 if( client_fd != -1 )
540 net_close( client_fd );
541
542 if( listen_fd != -1 )
543 net_close( listen_fd );
544
545#if defined(_WIN32)
546 printf( " Press Enter to exit this program.\n" );
547 fflush( stdout ); getchar();
548#endif
549
550 return( ret != 0 );
551}
552
553#endif /* POLARSSL_NET_C */