summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c73
1 files changed, 18 insertions, 55 deletions
diff --git a/opentracker.c b/opentracker.c
index 657f4f3..e286a24 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -40,12 +40,9 @@ static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
40static char static_inbuf[8192]; 40static char static_inbuf[8192];
41static char static_outbuf[8192]; 41static char static_outbuf[8192];
42 42
43#define OT_MAXSOCKETS_TCP4 64 43static char *FLAG_TCP = "TCP";
44#define OT_MAXSOCKETS_UDP4 64 44static char *FLAG_UDP = "UDP";
45static int64 ot_sockets_tcp4[ OT_MAXSOCKETS_TCP4 ]; 45static size_t ot_sockets_count = 0;
46static int64 ot_sockets_udp4[ OT_MAXSOCKETS_UDP4 ];
47static int ot_sockets_tcp4_count = 0;
48static int ot_sockets_udp4_count = 0;
49 46
50#ifdef _DEBUG_HTTPERROR 47#ifdef _DEBUG_HTTPERROR
51static char debug_request[8192]; 48static char debug_request[8192];
@@ -76,10 +73,7 @@ static void handle_read( const int64 clientsocket );
76static void handle_write( const int64 clientsocket ); 73static void handle_write( const int64 clientsocket );
77static void handle_udp4( const int64 serversocket ); 74static void handle_udp4( const int64 serversocket );
78 75
79static void ot_try_bind_udp4( char ip[4], uint16 port ); 76static void ot_try_bind( char ip[4], uint16 port, int is_tcp );
80static void ot_try_bind_tcp4( char ip[4], uint16 port );
81static int ot_in_udp4_sockets( int64 fd );
82static int ot_in_tcp4_sockets( int64 fd );
83 77
84static void usage( char *name ); 78static void usage( char *name );
85static void help( char *name ); 79static void help( char *name );
@@ -415,7 +409,7 @@ ANNOUNCE_WORKAROUND:
415 409
416 1. In order to avoid having two buffers, one for header and one for content, we allow all above functions from trackerlogic to 410 1. In order to avoid having two buffers, one for header and one for content, we allow all above functions from trackerlogic to
417 write to a fixed location, leaving SUCCESS_HTTP_HEADER_LENGTH bytes in our static buffer, which is enough for the static string 411 write to a fixed location, leaving SUCCESS_HTTP_HEADER_LENGTH bytes in our static buffer, which is enough for the static string
418 plus dynamic space needed to expand our Content-Length value. We reserve SUCCESS_HTTP_SIZE_OFF for it expansion and calculate 412 plus dynamic space needed to expand our Content-Length value. We reserve SUCCESS_HTTP_SIZE_OFF for its expansion and calculate
419 the space NOT needed to expand in reply_off 413 the space NOT needed to expand in reply_off
420 */ 414 */
421 reply_off = SUCCESS_HTTP_SIZE_OFF - snprintf( static_outbuf, 0, "%zd", reply_size ); 415 reply_off = SUCCESS_HTTP_SIZE_OFF - snprintf( static_outbuf, 0, "%zd", reply_size );
@@ -630,22 +624,6 @@ static void handle_udp4( int64 serversocket ) {
630 } 624 }
631} 625}
632 626
633static int ot_in_tcp4_sockets( int64 fd ) {
634 int i;
635 for( i=0; i<ot_sockets_tcp4_count; ++i)
636 if( ot_sockets_tcp4[i] == fd )
637 return 1;
638 return 0;
639}
640
641static int ot_in_udp4_sockets( int64 fd ) {
642 int i;
643 for( i=0; i<ot_sockets_udp4_count; ++i)
644 if( ot_sockets_udp4[i] == fd )
645 return 1;
646 return 0;
647}
648
649static void server_mainloop( ) { 627static void server_mainloop( ) {
650 tai6464 t, next_timeout_check; 628 tai6464 t, next_timeout_check;
651 629
@@ -659,9 +637,10 @@ static void server_mainloop( ) {
659 io_waituntil( t ); 637 io_waituntil( t );
660 638
661 while( ( i = io_canread( ) ) != -1 ) { 639 while( ( i = io_canread( ) ) != -1 ) {
662 if( ot_in_tcp4_sockets( i ) ) 640 const void *cookie = io_getcookie( i );
641 if( cookie == FLAG_TCP )
663 handle_accept( i ); 642 handle_accept( i );
664 else if( ot_in_udp4_sockets( i ) ) 643 else if( cookie == FLAG_UDP )
665 handle_udp4( i ); 644 handle_udp4( i );
666 else 645 else
667 handle_read( i ); 646 handle_read( i );
@@ -679,39 +658,23 @@ static void server_mainloop( ) {
679 } 658 }
680} 659}
681 660
682static void ot_try_bind_tcp4( char ip[4], uint16 port ) { 661static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) {
683 int64 s = socket_tcp4( ); 662 int64 s = is_tcp ? socket_tcp4( ) : socket_udp4();
684 if( ot_sockets_tcp4_count == OT_MAXSOCKETS_TCP4 ) { 663
685 fprintf( stderr, "Too many tcp4 sockets, increase OT_MAXSOCKETS_TCP4 and recompile.\n"); exit(1);
686 }
687 if( socket_bind4_reuse( s, ip, port ) == -1 ) 664 if( socket_bind4_reuse( s, ip, port ) == -1 )
688 panic( "socket_bind4_reuse" ); 665 panic( "socket_bind4_reuse" );
689 666
690 if( socket_listen( s, SOMAXCONN) == -1 ) 667 if( is_tcp && ( socket_listen( s, SOMAXCONN) == -1 ) )
691 panic( "socket_listen" ); 668 panic( "socket_listen" );
692 669
693 if( !io_fd( s ) ) 670 if( !io_fd( s ) )
694 panic( "io_fd" ); 671 panic( "io_fd" );
695 672
696 io_wantread( s ); 673 io_setcookie( s, is_tcp ? FLAG_TCP : FLAG_UDP );
697
698 ot_sockets_tcp4[ ot_sockets_tcp4_count++ ] = s;
699}
700
701static void ot_try_bind_udp4( char ip[4], uint16 port ) {
702 int64 s = socket_udp4( );
703 if( ot_sockets_udp4_count == OT_MAXSOCKETS_UDP4 ) {
704 fprintf( stderr, "Too many udp4 sockets, increase OT_MAXSOCKETS_UDP4 and recompile.\n"); exit(1);
705 }
706 if( socket_bind4_reuse( s, ip, port ) == -1 )
707 panic( "socket_bind4_reuse" );
708
709 if( !io_fd( s ) )
710 panic( "io_fd" );
711 674
712 io_wantread( s ); 675 io_wantread( s );
713 676
714 ot_sockets_udp4[ ot_sockets_udp4_count++ ] = s; 677 ++ot_sockets_count;
715} 678}
716 679
717int main( int argc, char **argv ) { 680int main( int argc, char **argv ) {
@@ -723,8 +686,8 @@ int main( int argc, char **argv ) {
723 switch( getopt( argc, argv, ":i:p:P:d:ocbBh" ) ) { 686 switch( getopt( argc, argv, ":i:p:P:d:ocbBh" ) ) {
724 case -1 : scanon = 0; break; 687 case -1 : scanon = 0; break;
725 case 'i': scan_ip4( optarg, serverip ); break; 688 case 'i': scan_ip4( optarg, serverip ); break;
726 case 'p': ot_try_bind_tcp4( serverip, (uint16)atol( optarg ) ); break; 689 case 'p': ot_try_bind( serverip, (uint16)atol( optarg ), 1 ); break;
727 case 'P': ot_try_bind_udp4( serverip, (uint16)atol( optarg ) ); break; 690 case 'P': ot_try_bind( serverip, (uint16)atol( optarg ), 0 ); break;
728 case 'd': serverdir = optarg; break; 691 case 'd': serverdir = optarg; break;
729 case 'h': help( argv[0] ); exit( 0 ); 692 case 'h': help( argv[0] ); exit( 0 );
730#ifdef WANT_CLOSED_TRACKER 693#ifdef WANT_CLOSED_TRACKER
@@ -741,8 +704,8 @@ int main( int argc, char **argv ) {
741 } 704 }
742 705
743 // Bind to our default tcp port 706 // Bind to our default tcp port
744 if( !ot_sockets_tcp4_count && !ot_sockets_udp4_count ) 707 if( !ot_sockets_count )
745 ot_try_bind_tcp4( serverip, 6969 ); 708 ot_try_bind( serverip, 6969, 1 );
746 709
747 setegid( (gid_t)-2 ); setuid( (uid_t)-2 ); 710 setegid( (gid_t)-2 ); setuid( (uid_t)-2 );
748 setgid( (gid_t)-2 ); seteuid( (uid_t)-2 ); 711 setgid( (gid_t)-2 ); seteuid( (uid_t)-2 );