udp_proxy: allow successive clients

This commit is contained in:
Manuel Pégourié-Gonnard 2014-09-23 12:46:33 +02:00 committed by Paul Bakker
parent 484b8f9ed8
commit 6312e0f4e6

View file

@ -327,11 +327,17 @@ int send_packet( const packet *p, const char *why )
return( 0 ); return( 0 );
} }
static packet prev;
void clear_pending( void )
{
memset( &prev, 0, sizeof( packet ) );
}
int handle_message( const char *way, int dst, int src ) int handle_message( const char *way, int dst, int src )
{ {
int ret; int ret;
packet cur; packet cur;
static packet prev;
/* receive packet */ /* receive packet */
if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 ) if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
@ -446,6 +452,7 @@ int main( int argc, char *argv[] )
/* /*
* 2. Wait until a client connects * 2. Wait until a client connects
*/ */
accept:
printf( " . Waiting for a remote connection ..." ); printf( " . Waiting for a remote connection ..." );
fflush( stdout ); fflush( stdout );
@ -458,16 +465,35 @@ int main( int argc, char *argv[] )
printf( " ok\n" ); printf( " ok\n" );
fflush( stdout ); fflush( stdout );
printf( " . Re-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" );
/* /*
* 3. Forward packets forever (kill the process to terminate it) * 3. Forward packets forever (kill the process to terminate it)
*/ */
nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1; nb_fds = client_fd;
if( nb_fds < server_fd )
nb_fds = server_fd;
if( nb_fds < listen_fd )
nb_fds = listen_fd;
++nb_fds;
while( 1 ) while( 1 )
{ {
FD_ZERO( &read_fds ); FD_ZERO( &read_fds );
FD_SET( server_fd, &read_fds ); FD_SET( server_fd, &read_fds );
FD_SET( client_fd, &read_fds ); FD_SET( client_fd, &read_fds );
FD_SET( listen_fd, &read_fds );
if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
{ {
@ -475,6 +501,12 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
if( FD_ISSET( listen_fd, &read_fds ) )
{
clear_pending();
goto accept;
}
if( FD_ISSET( client_fd, &read_fds ) ) if( FD_ISSET( client_fd, &read_fds ) )
{ {
if( ( ret = handle_message( "S <- C", if( ( ret = handle_message( "S <- C",