blob: 48549cff5622d615d1e61061c13fb4b4a9ef9f49 [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é-Gonnard7cf35182014-09-20 09:43:48 +020069/* For gettimeofday() */
70#if !defined(_WIN32)
71#include <sys/time.h>
72#endif
73
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020074#define MAX_MSG_SIZE 4096 /* Reasonable max size for our tests */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020075
76#define DFL_SERVER_ADDR "localhost"
77#define DFL_SERVER_PORT 4433
78#define DFL_LISTEN_ADDR "localhost"
79#define DFL_LISTEN_PORT 5556
80
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020081#define USAGE \
82 "\n usage: udp_proxy param=<>...\n" \
83 "\n acceptable parameters:\n" \
84 " server_addr=%%d default: localhost\n" \
85 " server_port=%%d default: 4433\n" \
86 " listen_addr=%%d default: localhost\n" \
87 " listen_port=%%d default: 4433\n" \
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +020088 "\n" \
89 " duplicate=%%d default: 0 (no duplication)\n" \
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +020090 " duplicate 1 packet every N packets\n" \
91 " delay=%%d default: 0 (no delayed packets)\n" \
92 " delay 1 packet every N packets\n" \
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +020093 " delay_ccs=%%d default: 0 (don't delay ChangeCipherSuite)\n" \
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +020094 " drop=%%d default: 0 (no dropped packets)\n" \
95 " drop 1 packet every N packets\n" \
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +020096 " mtu=%%d default: 0 (unlimited)\n" \
97 " drop packets larger than N bytes\n" \
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +020098 " bad_ad=%%d default: 0 (don't add bad ApplicationData)\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é-Gonnard44d5e632014-09-06 08:07:45 +0200117} opt;
118
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200119/*
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200120 * global counters
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200121 */
122static int dupl_cnt;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200123static int delay_cnt;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200124static int drop_cnt;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200125
126/* Do not always start with the same state */
127static void randomize_counters( void )
128{
129#if defined(POLARSSL_HAVE_TIME)
130 srand( time( NULL ) );
131#endif
132
133 if( opt.duplicate != 0 )
134 dupl_cnt = rand() % opt.duplicate;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200135 if( opt.delay != 0 )
136 delay_cnt = rand() % opt.delay;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200137 if( opt.drop != 0 )
138 drop_cnt = rand() % opt.drop;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200139}
140
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200141static void exit_usage( const char *name, const char *value )
142{
143 if( value == NULL )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200144 printf( " unknown option or missing value: %s\n", name );
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200145 else
146 printf( " option %s: illegal value: %s\n", name, value );
147
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200148 printf( USAGE );
149 exit( 1 );
150}
151
152static void get_options( int argc, char *argv[] )
153{
154 int i;
155 char *p, *q;
156
157 opt.server_addr = DFL_SERVER_ADDR;
158 opt.server_port = DFL_SERVER_PORT;
159 opt.listen_addr = DFL_LISTEN_ADDR;
160 opt.listen_port = DFL_LISTEN_PORT;
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200161 /* Other members default to 0 */
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200162
163 for( i = 1; i < argc; i++ )
164 {
165 p = argv[i];
166 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200167 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200168 *q++ = '\0';
169
170 if( strcmp( p, "server_addr" ) == 0 )
171 opt.server_addr = q;
172 else if( strcmp( p, "server_port" ) == 0 )
173 {
174 opt.server_port = atoi( q );
175 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200176 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200177 }
178 else if( strcmp( p, "listen_addr" ) == 0 )
179 opt.listen_addr = q;
180 else if( strcmp( p, "listen_port" ) == 0 )
181 {
182 opt.listen_port = atoi( q );
183 if( opt.listen_port < 1 || opt.listen_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200184 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200185 }
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200186 else if( strcmp( p, "duplicate" ) == 0 )
187 {
188 opt.duplicate = atoi( q );
189 if( opt.duplicate < 0 || opt.duplicate > 10 )
190 exit_usage( p, q );
191 }
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200192 else if( strcmp( p, "delay" ) == 0 )
193 {
194 opt.delay = atoi( q );
195 if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
196 exit_usage( p, q );
197 }
Manuel Pégourié-Gonnard81f2fe92014-09-08 10:44:57 +0200198 else if( strcmp( p, "delay_ccs" ) == 0 )
199 {
200 opt.delay_ccs = atoi( q );
201 if( opt.delay_ccs < 0 || opt.delay_ccs > 1 )
202 exit_usage( p, q );
203 }
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200204 else if( strcmp( p, "drop" ) == 0 )
205 {
206 opt.drop = atoi( q );
207 if( opt.drop < 0 || opt.drop > 10 || opt.drop == 1 )
208 exit_usage( p, q );
209 }
Manuel Pégourié-Gonnardeb00bfd2014-09-08 11:11:42 +0200210 else if( strcmp( p, "mtu" ) == 0 )
211 {
212 opt.mtu = atoi( q );
213 if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE )
214 exit_usage( p, q );
215 }
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200216 else if( strcmp( p, "bad_ad" ) == 0 )
217 {
218 opt.bad_ad = atoi( q );
219 if( opt.bad_ad < 0 || opt.bad_ad > 1 )
220 exit_usage( p, q );
221 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200222 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200223 exit_usage( p, NULL );
224 }
225}
226
227static const char *msg_type( unsigned char *msg, size_t len )
228{
229 if( len < 1 ) return( "Invalid" );
230 switch( msg[0] )
231 {
232 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
233 case SSL_MSG_ALERT: return( "Alert" );
234 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
235 case SSL_MSG_HANDSHAKE: break; /* See below */
236 default: return( "Unknown" );
237 }
238
239 if( len < 13 ) return( "Invalid handshake" );
240 switch( msg[13] )
241 {
242 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
243 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
244 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
245 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
246 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
247 case SSL_HS_CERTIFICATE: return( "Certificate" );
248 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
249 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
250 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
251 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
252 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
253 case SSL_HS_FINISHED: return( "Finished" );
254 default: return( "Unkown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200255 }
256}
257
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200258/* Return elapsed time in milliseconds since the first call */
259static unsigned long ellapsed_time( void )
260{
261#if defined(_WIN32)
262 return( 0 );
263#else
264 static struct timeval ref = { 0, 0 };
265 struct timeval now;
266
267 if( ref.tv_sec == 0 && ref.tv_usec == 0 )
268 {
269 gettimeofday( &ref, NULL );
270 return( 0 );
271 }
272
273 gettimeofday( &now, NULL );
274 return( 1000 * ( now.tv_sec - ref.tv_sec )
275 + ( now.tv_usec - ref.tv_usec ) / 1000 );
276#endif
277}
278
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200279typedef struct
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200280{
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200281 void *dst;
282 const char *way;
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200283 const char *type;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200284 unsigned len;
285 unsigned char buf[MAX_MSG_SIZE];
286} packet;
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200287
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200288/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
289void print_packet( const packet *p, const char *why )
290{
291 if( why == NULL )
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200292 printf( " %05lu %s %s (%u bytes)\n",
293 ellapsed_time(), p->way, p->type, p->len );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200294 else
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200295 printf( " %s %s (%u bytes): %s\n",
296 p->way, p->type, p->len, why );
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200297 fflush( stdout );
298}
299
300int send_packet( const packet *p, const char *why )
301{
302 int ret;
303
Manuel Pégourié-Gonnard6c18a392014-09-08 11:24:58 +0200304 /* insert corrupted ApplicationData record? */
305 if( opt.bad_ad &&
306 strcmp( p->type, "ApplicationData" ) == 0 )
307 {
308 unsigned char buf[MAX_MSG_SIZE];
309 memcpy( buf, p->buf, p->len );
310 ++buf[p->len - 1];
311
312 print_packet( p, "corrupted" );
313 if( ( ret = net_send( p->dst, buf, p->len ) ) <= 0 )
314 {
315 printf( " ! net_send returned %d\n", ret );
316 return( ret );
317 }
318 }
319
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200320 print_packet( p, why );
321 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200322 {
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200323 printf( " ! net_send returned %d\n", ret );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200324 return( ret );
325 }
326
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200327 /* Don't duplicate Application Data, only handshake covered */
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200328 if( opt.duplicate != 0 &&
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200329 strcmp( p->type, "ApplicationData" ) != 0 &&
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200330 ++dupl_cnt == opt.duplicate )
331 {
332 dupl_cnt = 0;
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200333 print_packet( p, "duplicated" );
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200334
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200335 if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
Manuel Pégourié-Gonnard2c41bd82014-09-06 08:14:47 +0200336 {
337 printf( " ! net_send returned %d\n", ret );
338 return( ret );
339 }
340 }
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200341
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200342 return( 0 );
343}
344
345int handle_message( const char *way, int dst, int src )
346{
347 int ret;
348 packet cur;
349 static packet prev;
350
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;
361 cur.dst = &dst;
362 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 &&
369 ++drop_cnt == opt.drop ) )
Manuel Pégourié-Gonnard60fdd7e2014-09-06 14:49:52 +0200370 {
371 drop_cnt = 0;
372 }
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 &&
377 ++delay_cnt == opt.delay ) )
Manuel Pégourié-Gonnard21398c32014-09-06 14:36:46 +0200378 {
379 delay_cnt = 0;
380 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 */
389 if( prev.dst != NULL )
390 {
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é-Gonnard2c41bd82014-09-06 08:14:47 +0200413 randomize_counters();
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200414
415 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200416 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200417 */
418 printf( " . Connect to server on UDP/%s/%d ...",
419 opt.server_addr, opt.server_port );
420 fflush( stdout );
421
422 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
423 NET_PROTO_UDP ) ) != 0 )
424 {
425 printf( " failed\n ! net_connect returned %d\n\n", ret );
426 goto exit;
427 }
428
429 printf( " ok\n" );
430
431 /*
432 * 1. Setup the "listening" UDP socket
433 */
434 printf( " . Bind on UDP/%s/%d ...",
435 opt.listen_addr, opt.listen_port );
436 fflush( stdout );
437
438 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
439 NET_PROTO_UDP ) ) != 0 )
440 {
441 printf( " failed\n ! net_bind returned %d\n\n", ret );
442 goto exit;
443 }
444
445 printf( " ok\n" );
446
447 /*
448 * 2. Wait until a client connects
449 */
450 printf( " . Waiting for a remote connection ..." );
451 fflush( stdout );
452
453 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
454 {
455 printf( " failed\n ! net_accept returned %d\n\n", ret );
456 goto exit;
457 }
458
459 printf( " ok\n" );
460 fflush( stdout );
461
462 /*
463 * 3. Forward packets forever (kill the process to terminate it)
464 */
465 nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
466
467 while( 1 )
468 {
469 FD_ZERO( &read_fds );
470 FD_SET( server_fd, &read_fds );
471 FD_SET( client_fd, &read_fds );
472
473 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
474 {
475 perror( "select" );
476 goto exit;
477 }
478
479 if( FD_ISSET( client_fd, &read_fds ) )
480 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200481 if( ( ret = handle_message( "S <- C",
482 server_fd, client_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200483 goto exit;
484 }
485
486 if( FD_ISSET( server_fd, &read_fds ) )
487 {
Manuel Pégourié-Gonnard7cf35182014-09-20 09:43:48 +0200488 if( ( ret = handle_message( "S -> C",
489 client_fd, server_fd ) ) != 0 )
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200490 goto exit;
491 }
492 }
493
494exit:
495
496#ifdef POLARSSL_ERROR_C
497 if( ret != 0 )
498 {
499 char error_buf[100];
500 polarssl_strerror( ret, error_buf, 100 );
501 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
502 fflush( stdout );
503 }
504#endif
505
506 if( client_fd != -1 )
507 net_close( client_fd );
508
509 if( listen_fd != -1 )
510 net_close( listen_fd );
511
512#if defined(_WIN32)
513 printf( " Press Enter to exit this program.\n" );
514 fflush( stdout ); getchar();
515#endif
516
517 return( ret != 0 );
518}
519
520#endif /* POLARSSL_NET_C */