mbedtls/programs/test/udp_proxy.c

429 lines
12 KiB
C
Raw Normal View History

/*
* UDP proxy: emulate an unreliable UDP connexion for DTLS testing
*
* Copyright (C) 2006-2014, Brainspark B.V.
*
* This file is part of PolarSSL (http://www.polarssl.org)
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
*
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if !defined(POLARSSL_CONFIG_FILE)
#include "polarssl/config.h"
#else
#include POLARSSL_CONFIG_FILE
#endif
#if !defined(POLARSSL_NET_C)
#include <stdio.h>
int main( void )
{
printf( "POLARSSL_NET_C not defined.\n" );
return( 0 );
}
#else
#include "polarssl/net.h"
#include "polarssl/error.h"
2014-09-06 06:14:47 +00:00
#include "polarssl/ssl.h"
#include <stdio.h>
#include <stdlib.h>
2014-09-06 06:14:47 +00:00
#if defined(POLARSSL_HAVE_TIME)
#include <time.h>
#endif
/* For select() */
#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
!defined(EFI32)
#include <winsock2.h>
#include <windows.h>
#if defined(_MSC_VER)
#if defined(_WIN32_WCE)
#pragma comment( lib, "ws2.lib" )
#else
#pragma comment( lib, "ws2_32.lib" )
#endif
#endif /* _MSC_VER */
#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
2014-09-06 12:36:46 +00:00
#define MAX_MSG_SIZE 4096 /* Reasonable max size for our tests */
#define DFL_SERVER_ADDR "localhost"
#define DFL_SERVER_PORT 4433
#define DFL_LISTEN_ADDR "localhost"
#define DFL_LISTEN_PORT 5556
2014-09-06 06:07:45 +00:00
#define USAGE \
"\n usage: udp_proxy param=<>...\n" \
"\n acceptable parameters:\n" \
" server_addr=%%d default: localhost\n" \
" server_port=%%d default: 4433\n" \
" listen_addr=%%d default: localhost\n" \
" listen_port=%%d default: 4433\n" \
2014-09-06 06:14:47 +00:00
"\n" \
" duplicate=%%d default: 0 (no duplication)\n" \
2014-09-06 12:36:46 +00:00
" duplicate 1 packet every N packets\n" \
" delay=%%d default: 0 (no delayed packets)\n" \
" delay 1 packet every N packets\n" \
2014-09-06 06:07:45 +00:00
"\n"
/*
* global options
*/
2014-09-06 06:07:45 +00:00
static struct options
{
const char *server_addr; /* address to forward packets to */
int server_port; /* port to forward packets to */
const char *listen_addr; /* address for accepting client connections */
int listen_port; /* port for accepting client connections */
2014-09-06 06:14:47 +00:00
int duplicate; /* duplicate 1 in N packets (none if 0) */
2014-09-06 12:36:46 +00:00
int delay; /* delay 1 packet in N (none if 0) */
} opt;
2014-09-06 06:14:47 +00:00
/*
2014-09-06 12:36:46 +00:00
* global counters
2014-09-06 06:14:47 +00:00
*/
static int dupl_cnt;
2014-09-06 12:36:46 +00:00
static int delay_cnt;
2014-09-06 06:14:47 +00:00
/* Do not always start with the same state */
static void randomize_counters( void )
{
#if defined(POLARSSL_HAVE_TIME)
srand( time( NULL ) );
#endif
if( opt.duplicate != 0 )
dupl_cnt = rand() % opt.duplicate;
2014-09-06 12:36:46 +00:00
if( opt.delay != 0 )
delay_cnt = rand() % opt.delay;
2014-09-06 06:14:47 +00:00
}
2014-09-06 06:07:45 +00:00
static void exit_usage( const char *name, const char *value )
{
2014-09-06 06:07:45 +00:00
if( value == NULL )
2014-09-06 12:36:46 +00:00
printf( " unknown option or missing value: %s\n", name );
2014-09-06 06:07:45 +00:00
else
printf( " option %s: illegal value: %s\n", name, value );
printf( USAGE );
exit( 1 );
}
static void get_options( int argc, char *argv[] )
{
int i;
char *p, *q;
opt.server_addr = DFL_SERVER_ADDR;
opt.server_port = DFL_SERVER_PORT;
opt.listen_addr = DFL_LISTEN_ADDR;
opt.listen_port = DFL_LISTEN_PORT;
for( i = 1; i < argc; i++ )
{
p = argv[i];
if( ( q = strchr( p, '=' ) ) == NULL )
2014-09-06 06:07:45 +00:00
exit_usage( p, NULL );
*q++ = '\0';
if( strcmp( p, "server_addr" ) == 0 )
opt.server_addr = q;
else if( strcmp( p, "server_port" ) == 0 )
{
opt.server_port = atoi( q );
if( opt.server_port < 1 || opt.server_port > 65535 )
2014-09-06 06:07:45 +00:00
exit_usage( p, q );
}
else if( strcmp( p, "listen_addr" ) == 0 )
opt.listen_addr = q;
else if( strcmp( p, "listen_port" ) == 0 )
{
opt.listen_port = atoi( q );
if( opt.listen_port < 1 || opt.listen_port > 65535 )
2014-09-06 06:07:45 +00:00
exit_usage( p, q );
}
2014-09-06 06:14:47 +00:00
else if( strcmp( p, "duplicate" ) == 0 )
{
opt.duplicate = atoi( q );
if( opt.duplicate < 0 || opt.duplicate > 10 )
exit_usage( p, q );
}
2014-09-06 12:36:46 +00:00
else if( strcmp( p, "delay" ) == 0 )
{
opt.delay = atoi( q );
if( opt.delay < 0 || opt.delay > 10 || opt.delay == 1 )
exit_usage( p, q );
}
else
2014-09-06 06:07:45 +00:00
exit_usage( p, NULL );
}
}
static const char *msg_type( unsigned char *msg, size_t len )
{
if( len < 1 ) return( "Invalid" );
switch( msg[0] )
{
case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
case SSL_MSG_ALERT: return( "Alert" );
case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
case SSL_MSG_HANDSHAKE: break; /* See below */
default: return( "Unknown" );
}
if( len < 13 ) return( "Invalid handshake" );
switch( msg[13] )
{
case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
case SSL_HS_SERVER_HELLO: return( "ServerHello" );
case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
case SSL_HS_CERTIFICATE: return( "Certificate" );
case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
case SSL_HS_FINISHED: return( "Finished" );
default: return( "Unkown handshake" );
}
}
2014-09-06 12:36:46 +00:00
typedef struct
{
2014-09-06 12:36:46 +00:00
void *dst;
const char *way;
2014-09-06 06:14:47 +00:00
const char *type;
2014-09-06 12:36:46 +00:00
unsigned len;
unsigned char buf[MAX_MSG_SIZE];
} packet;
/* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */
void print_packet( const packet *p, const char *why )
{
if( why == NULL )
printf( " > %s: %s (%u bytes)\n", p->way, p->type, p->len );
else
printf( " < %s: %s (%u bytes): %s\n", p->way, p->type, p->len, why );
fflush( stdout );
}
int send_packet( const packet *p, const char *why )
{
int ret;
2014-09-06 12:36:46 +00:00
print_packet( p, why );
if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
{
2014-09-06 12:36:46 +00:00
printf( " ! net_send returned %d\n", ret );
return( ret );
}
2014-09-06 06:14:47 +00:00
/* Don't duplicate Application Data, only handshake covered */
// Don't duplicate CSS for now (TODO later)
if( opt.duplicate != 0 &&
2014-09-06 12:36:46 +00:00
strcmp( p->type, "ApplicationData" ) != 0 &&
strcmp( p->type, "ChangeCipherSpec" ) != 0 &&
2014-09-06 06:14:47 +00:00
++dupl_cnt == opt.duplicate )
{
dupl_cnt = 0;
2014-09-06 12:36:46 +00:00
print_packet( p, "duplicated" );
2014-09-06 06:14:47 +00:00
2014-09-06 12:36:46 +00:00
if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 )
2014-09-06 06:14:47 +00:00
{
printf( " ! net_send returned %d\n", ret );
return( ret );
}
}
2014-09-06 12:36:46 +00:00
return( 0 );
}
int handle_message( const char *way, int dst, int src )
{
int ret;
packet cur;
static packet prev;
/* receivec packet */
if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
{
2014-09-06 12:36:46 +00:00
printf( " ! net_recv returned %d\n", ret );
return( ret );
}
2014-09-06 12:36:46 +00:00
cur.len = ret;
cur.type = msg_type( cur.buf, cur.len );
cur.way = way;
cur.dst = &dst;
print_packet( &cur, NULL );
/* do we want to delay it? */
if( opt.delay != 0 &&
strcmp( cur.type, "ApplicationData" ) != 0 &&
++delay_cnt == opt.delay )
{
delay_cnt = 0;
memcpy( &prev, &cur, sizeof( packet ) );
}
else
{
/* not delayed: forward (and possibly duplicate) it */
if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 )
return( ret );
/* send previously delayed message if any */
if( prev.dst != NULL )
{
ret = send_packet( &prev, "delayed" );
memset( &prev, 0, sizeof( packet ) );
if( ret != 0 )
return( ret );
}
}
return( 0 );
}
int main( int argc, char *argv[] )
{
int ret;
int listen_fd = -1;
int client_fd = -1;
int server_fd = -1;
int nb_fds;
fd_set read_fds;
get_options( argc, argv );
2014-09-06 06:14:47 +00:00
randomize_counters();
/*
2014-09-06 06:07:45 +00:00
* 0. "Connect" to the server
*/
printf( " . Connect to server on UDP/%s/%d ...",
opt.server_addr, opt.server_port );
fflush( stdout );
if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_connect returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
/*
* 1. Setup the "listening" UDP socket
*/
printf( " . Bind on UDP/%s/%d ...",
opt.listen_addr, opt.listen_port );
fflush( stdout );
if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_bind returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
/*
* 2. Wait until a client connects
*/
printf( " . Waiting for a remote connection ..." );
fflush( stdout );
if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
{
printf( " failed\n ! net_accept returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
fflush( stdout );
/*
* 3. Forward packets forever (kill the process to terminate it)
*/
nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
while( 1 )
{
FD_ZERO( &read_fds );
FD_SET( server_fd, &read_fds );
FD_SET( client_fd, &read_fds );
if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
{
perror( "select" );
goto exit;
}
if( FD_ISSET( client_fd, &read_fds ) )
{
if( ( ret = handle_message( "c2s", server_fd, client_fd ) ) != 0 )
goto exit;
}
if( FD_ISSET( server_fd, &read_fds ) )
{
if( ( ret = handle_message( "s2c", client_fd, server_fd ) ) != 0 )
goto exit;
}
}
exit:
#ifdef POLARSSL_ERROR_C
if( ret != 0 )
{
char error_buf[100];
polarssl_strerror( ret, error_buf, 100 );
printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
fflush( stdout );
}
#endif
if( client_fd != -1 )
net_close( client_fd );
if( listen_fd != -1 )
net_close( listen_fd );
#if defined(_WIN32)
printf( " Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
return( ret != 0 );
}
#endif /* POLARSSL_NET_C */