mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-02-26 12:39:55 +00:00
Add option 'drop' to udp_proxy
This commit is contained in:
parent
21398c37c0
commit
60fdd7e0f2
@ -85,6 +85,8 @@ int main( void )
|
|||||||
" duplicate 1 packet every N packets\n" \
|
" duplicate 1 packet every N packets\n" \
|
||||||
" delay=%%d default: 0 (no delayed packets)\n" \
|
" delay=%%d default: 0 (no delayed packets)\n" \
|
||||||
" delay 1 packet every N packets\n" \
|
" delay 1 packet every N packets\n" \
|
||||||
|
" drop=%%d default: 0 (no dropped packets)\n" \
|
||||||
|
" drop 1 packet every N packets\n" \
|
||||||
"\n"
|
"\n"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -99,6 +101,7 @@ static struct options
|
|||||||
|
|
||||||
int duplicate; /* duplicate 1 in N packets (none if 0) */
|
int duplicate; /* duplicate 1 in N packets (none if 0) */
|
||||||
int delay; /* delay 1 packet in N (none if 0) */
|
int delay; /* delay 1 packet in N (none if 0) */
|
||||||
|
int drop; /* drop 1 packet in N (none if 0) */
|
||||||
} opt;
|
} opt;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -106,6 +109,7 @@ static struct options
|
|||||||
*/
|
*/
|
||||||
static int dupl_cnt;
|
static int dupl_cnt;
|
||||||
static int delay_cnt;
|
static int delay_cnt;
|
||||||
|
static int drop_cnt;
|
||||||
|
|
||||||
/* Do not always start with the same state */
|
/* Do not always start with the same state */
|
||||||
static void randomize_counters( void )
|
static void randomize_counters( void )
|
||||||
@ -118,6 +122,8 @@ static void randomize_counters( void )
|
|||||||
dupl_cnt = rand() % opt.duplicate;
|
dupl_cnt = rand() % opt.duplicate;
|
||||||
if( opt.delay != 0 )
|
if( opt.delay != 0 )
|
||||||
delay_cnt = rand() % opt.delay;
|
delay_cnt = rand() % opt.delay;
|
||||||
|
if( opt.drop != 0 )
|
||||||
|
drop_cnt = rand() % opt.drop;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void exit_usage( const char *name, const char *value )
|
static void exit_usage( const char *name, const char *value )
|
||||||
@ -140,6 +146,7 @@ static void get_options( int argc, char *argv[] )
|
|||||||
opt.server_port = DFL_SERVER_PORT;
|
opt.server_port = DFL_SERVER_PORT;
|
||||||
opt.listen_addr = DFL_LISTEN_ADDR;
|
opt.listen_addr = DFL_LISTEN_ADDR;
|
||||||
opt.listen_port = DFL_LISTEN_PORT;
|
opt.listen_port = DFL_LISTEN_PORT;
|
||||||
|
/* Other members default to 0 */
|
||||||
|
|
||||||
for( i = 1; i < argc; i++ )
|
for( i = 1; i < argc; i++ )
|
||||||
{
|
{
|
||||||
@ -176,6 +183,12 @@ static void get_options( int argc, char *argv[] )
|
|||||||
if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
|
if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
|
||||||
exit_usage( p, q );
|
exit_usage( p, q );
|
||||||
}
|
}
|
||||||
|
else if( strcmp( p, "drop" ) == 0 )
|
||||||
|
{
|
||||||
|
opt.drop = atoi( q );
|
||||||
|
if( opt.drop < 0 || opt.drop > 10 || opt.drop == 1 )
|
||||||
|
exit_usage( p, q );
|
||||||
|
}
|
||||||
else
|
else
|
||||||
exit_usage( p, NULL );
|
exit_usage( p, NULL );
|
||||||
}
|
}
|
||||||
@ -281,8 +294,14 @@ int handle_message( const char *way, int dst, int src )
|
|||||||
cur.dst = &dst;
|
cur.dst = &dst;
|
||||||
print_packet( &cur, NULL );
|
print_packet( &cur, NULL );
|
||||||
|
|
||||||
/* do we want to delay it? */
|
/* do we want to drop, delay, or forward it? */
|
||||||
if( opt.delay != 0 &&
|
if( opt.drop != 0 &&
|
||||||
|
strcmp( cur.type, "ApplicationData" ) != 0 &&
|
||||||
|
++drop_cnt == opt.drop )
|
||||||
|
{
|
||||||
|
drop_cnt = 0;
|
||||||
|
}
|
||||||
|
else if( opt.delay != 0 &&
|
||||||
strcmp( cur.type, "ApplicationData" ) != 0 &&
|
strcmp( cur.type, "ApplicationData" ) != 0 &&
|
||||||
++delay_cnt == opt.delay )
|
++delay_cnt == opt.delay )
|
||||||
{
|
{
|
||||||
@ -291,7 +310,7 @@ int handle_message( const char *way, int dst, int src )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* not delayed: forward (and possibly duplicate) it */
|
/* forward and possibly duplicate */
|
||||||
if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
|
if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
|
||||||
return( ret );
|
return( ret );
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user