blob: da90b1a367789f05f4b7aca16c2bde9d765fe824 [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é-Gonnard21398c32014-09-06 14:36:46 +020072#define MAX_MSG_SIZE 4096 /* Reasonable max size for our tests */
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é-Gonnard21398c32014-09-06 14:36:46 +0200267 void *dst;
268 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;
289
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200290 /* insert corrupted ApplicationData record? */
291 if( opt.bad_ad &&
292 strcmp( p->type, "ApplicationData" ) == 0 )
293 {
294 unsigned char buf[MAX_MSG_SIZE];
295 memcpy( buf, p->buf, p->len );
296 ++buf[p->len - 1];
297
298 print_packet( p, "corrupted" );
299 if( ( ret = net_send( p->dst, buf, p->len ) ) <= 0 )
300 {
301 printf( " ! net_send returned %d\n", ret );
302 return( ret );
303 }
304 }
305
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200306 print_packet( p, why );
307 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200308 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200309 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200310 return( ret );
311 }
312
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200313 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200314 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200315 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200316 rand() % opt.duplicate == 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200317 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200318 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200319
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200320 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200321 {
322 printf( " ! net_send returned %d\n", ret );
323 return( ret );
324 }
325 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200326
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200327 return( 0 );
328}
329
330int handle_message( const char *way, int dst, int src )
331{
332 int ret;
333 packet cur;
334 static packet prev;
335
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +0200336 /* receive packet */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200337 if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200338 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200339 printf( " ! net_recv returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200340 return( ret );
341 }
342
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200343 cur.len = ret;
344 cur.type = msg_type( cur.buf, cur.len );
345 cur.way = way;
346 cur.dst = &dst;
347 print_packet( &cur, NULL );
348
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200349 /* do we want to drop, delay, or forward it? */
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200350 if( ( opt.mtu != 0 &&
351 cur.len > (unsigned) opt.mtu ) ||
352 ( opt.drop != 0 &&
353 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200354 rand() % opt.drop == 0 ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200355 {
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200356 ; /* Nothing to do */
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200357 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200358 else if( ( opt.delay_ccs == 1 &&
359 strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) ||
360 ( opt.delay != 0 &&
361 strcmp( cur.type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200362 rand() % opt.delay == 0 ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200363 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200364 memcpy( &prev, &cur, sizeof( packet ) );
365 }
366 else
367 {
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200368 /* forward and possibly duplicate */
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200369 if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
370 return( ret );
371
372 /* send previously delayed message if any */
373 if( prev.dst != NULL )
374 {
375 ret = send_packet( &prev, "delayed" );
376 memset( &prev, 0, sizeof( packet ) );
377 if( ret != 0 )
378 return( ret );
379 }
380 }
381
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200382 return( 0 );
383}
384
385int main( int argc, char *argv[] )
386{
387 int ret;
388
389 int listen_fd = -1;
390 int client_fd = -1;
391 int server_fd = -1;
392
393 int nb_fds;
394 fd_set read_fds;
395
396 get_options( argc, argv );
Manuel Pégourié-Gonnard992e1362014-09-20 18:06:23 +0200397
398 /*
399 * Decisions to drop/delay/duplicate packets are pseudo-random: dropping
400 * exactly 1 in N packets would lead to problems when a flight has exactly
401 * N packets: the same packet would be dropped on every resend.
402 *
403 * In order to be able to reproduce problems reliably, the seed may be
404 * specified explicitly.
405 */
406 if( opt.seed == 0 )
407 {
408 opt.seed = time( NULL );
409 printf( " . Pseudo-random seed: %u\n", opt.seed );
410 }
411
412 srand( opt.seed );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200413
414 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200415 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200416 */
417 printf( " . Connect to server on UDP/%s/%d ...",
418 opt.server_addr, opt.server_port );
419 fflush( stdout );
420
421 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
422 NET_PROTO_UDP ) ) != 0 )
423 {
424 printf( " failed\n ! net_connect returned %d\n\n", ret );
425 goto exit;
426 }
427
428 printf( " ok\n" );
429
430 /*
431 * 1. Setup the "listening" UDP socket
432 */
433 printf( " . Bind on UDP/%s/%d ...",
434 opt.listen_addr, opt.listen_port );
435 fflush( stdout );
436
437 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
438 NET_PROTO_UDP ) ) != 0 )
439 {
440 printf( " failed\n ! net_bind returned %d\n\n", ret );
441 goto exit;
442 }
443
444 printf( " ok\n" );
445
446 /*
447 * 2. Wait until a client connects
448 */
449 printf( " . Waiting for a remote connection ..." );
450 fflush( stdout );
451
452 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
453 {
454 printf( " failed\n ! net_accept returned %d\n\n", ret );
455 goto exit;
456 }
457
458 printf( " ok\n" );
459 fflush( stdout );
460
461 /*
462 * 3. Forward packets forever (kill the process to terminate it)
463 */
464 nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
465
466 while( 1 )
467 {
468 FD_ZERO( &read_fds );
469 FD_SET( server_fd, &read_fds );
470 FD_SET( client_fd, &read_fds );
471
472 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
473 {
474 perror( "select" );
475 goto exit;
476 }
477
478 if( FD_ISSET( client_fd, &read_fds ) )
479 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200480 if( ( ret = handle_message( "S <- C",
481 server_fd, client_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200482 goto exit;
483 }
484
485 if( FD_ISSET( server_fd, &read_fds ) )
486 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200487 if( ( ret = handle_message( "S -> C",
488 client_fd, server_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200489 goto exit;
490 }
491 }
492
493exit:
494
495#ifdef POLARSSL_ERROR_C
496 if( ret != 0 )
497 {
498 char error_buf[100];
499 polarssl_strerror( ret, error_buf, 100 );
500 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
501 fflush( stdout );
502 }
503#endif
504
505 if( client_fd != -1 )
506 net_close( client_fd );
507
508 if( listen_fd != -1 )
509 net_close( listen_fd );
510
511#if defined(_WIN32)
512 printf( " Press Enter to exit this program.\n" );
513 fflush( stdout ); getchar();
514#endif
515
516 return( ret != 0 );
517}
518
519#endif /* POLARSSL_NET_C */