summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c107
-rw-r--r--trackerlogic.c4
2 files changed, 91 insertions, 20 deletions
diff --git a/opentracker.c b/opentracker.c
index 090b382..51a5b33 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -12,6 +12,8 @@
12#include "case.h" 12#include "case.h"
13#include "fmt.h" 13#include "fmt.h"
14#include "str.h" 14#include "str.h"
15#include "scan.h"
16#include "ip4.h"
15#include <string.h> 17#include <string.h>
16#include <sys/types.h> 18#include <sys/types.h>
17#include <sys/stat.h> 19#include <sys/stat.h>
@@ -38,6 +40,13 @@ static const size_t SUCCESS_HTTP_SIZE_OFF = 17;
38static char static_inbuf[8192]; 40static char static_inbuf[8192];
39static char static_outbuf[8192]; 41static char static_outbuf[8192];
40 42
43#define OT_MAXSOCKETS_TCP4 64
44#define OT_MAXSOCKETS_UDP4 64
45static int64 ot_sockets_tcp4[ OT_MAXSOCKETS_TCP4 ];
46static int64 ot_sockets_udp4[ OT_MAXSOCKETS_UDP4 ];
47static int ot_sockets_tcp4_count = 0;
48static int ot_sockets_udp4_count = 0;
49
41#ifdef _DEBUG_HTTPERROR 50#ifdef _DEBUG_HTTPERROR
42static char debug_request[8192]; 51static char debug_request[8192];
43#endif 52#endif
@@ -60,7 +69,7 @@ static void httpresponse( const int64 s, char *data );
60static void sendmallocdata( const int64 s, char *buffer, const size_t size ); 69static void sendmallocdata( const int64 s, char *buffer, const size_t size );
61static void senddata( const int64 s, char *buffer, const size_t size ); 70static void senddata( const int64 s, char *buffer, const size_t size );
62 71
63static void server_mainloop( const int64 serversocket ); 72static void server_mainloop( );
64static void handle_timeouted( void ); 73static void handle_timeouted( void );
65static void handle_accept( const int64 serversocket ); 74static void handle_accept( const int64 serversocket );
66static void handle_read( const int64 clientsocket ); 75static void handle_read( const int64 clientsocket );
@@ -436,8 +445,9 @@ static void usage( char *name ) {
436 445
437static void help( char *name ) { 446static void help( char *name ) {
438 usage( name ); 447 usage( name );
439 fprintf( stderr, "\t-i serverip\tspecify ip to bind to (default: *)\n" 448 fprintf( stderr, "\t-i serverip\tspecify ip to bind to (default: *, you may specify more than one)\n"
440 "\t-p serverport\tspecify port to bind to (default: 6969)\n" 449 "\t-p serverport\tspecify port to bind to (default: 6969, you may specify more than one)\n"
450 "\t-P serverport\tspecify port to bind to (you may specify more than one)\n"
441 "\t-d serverdir\tspecify directory containing white- or black listed torrent info_hashes (default: \".\")\n" 451 "\t-d serverdir\tspecify directory containing white- or black listed torrent info_hashes (default: \".\")\n"
442#ifdef WANT_CLOSED_TRACKER 452#ifdef WANT_CLOSED_TRACKER
443 "\t-o\t\tmake tracker an open tracker, e.g. do not check for white list (default: off)\n" 453 "\t-o\t\tmake tracker an open tracker, e.g. do not check for white list (default: off)\n"
@@ -456,6 +466,7 @@ static void help( char *name ) {
456#endif 466#endif
457 "* To white list a torrent, touch a file inside serverdir with info_hash hex string, preprended by '-'.\n" 467 "* To white list a torrent, touch a file inside serverdir with info_hash hex string, preprended by '-'.\n"
458#endif 468#endif
469 "\nExample: ./opentracker -i 127.0.0.1 -p 6968 -P 6968 -i 10.1.1.23 -p 6969 -p 6970\n"
459); 470);
460} 471}
461 472
@@ -544,10 +555,35 @@ static void handle_timeouted( void ) {
544 } 555 }
545} 556}
546 557
547static void server_mainloop( const int64 serversocket ) { 558void handle_udp4( int64 serversocket ) {
559 size_t r;
560 char remoteip[4];
561 uint16 port;
562
563 r = socket_recv4(serversocket, static_inbuf, 8192, remoteip, &port);
564
565 // too lazy :)
566}
567
568int ot_in_tcp4_sockets( int64 fd ) {
569 int i;
570 for( i=0; i<ot_sockets_tcp4_count; ++i)
571 if( ot_sockets_tcp4[i] == fd )
572 return 1;
573 return 0;
574}
575
576int ot_in_udp4_sockets( int64 fd ) {
577 int i;
578 for( i=0; i<ot_sockets_udp4_count; ++i)
579 if( ot_sockets_udp4[i] == fd )
580 return 1;
581 return 0;
582}
583
584static void server_mainloop( ) {
548 tai6464 t, next_timeout_check; 585 tai6464 t, next_timeout_check;
549 586
550 io_wantread( serversocket );
551 taia_now( &next_timeout_check ); 587 taia_now( &next_timeout_check );
552 588
553 for( ; ; ) { 589 for( ; ; ) {
@@ -558,8 +594,10 @@ static void server_mainloop( const int64 serversocket ) {
558 io_waituntil( t ); 594 io_waituntil( t );
559 595
560 while( ( i = io_canread( ) ) != -1 ) { 596 while( ( i = io_canread( ) ) != -1 ) {
561 if( i == serversocket ) 597 if( ot_in_tcp4_sockets( i ) )
562 handle_accept( i ); 598 handle_accept( i );
599 else if( ot_in_udp4_sockets( i ) )
600 handle_udp4( i );
563 else 601 else
564 handle_read( i ); 602 handle_read( i );
565 } 603 }
@@ -576,18 +614,52 @@ static void server_mainloop( const int64 serversocket ) {
576 } 614 }
577} 615}
578 616
579int main( int argc, char **argv ) { 617void ot_try_bind_tcp4( char ip[4], uint16 port ) {
580 int64 s = socket_tcp4( ); 618 int64 s = socket_tcp4( );
581 char *serverip = NULL; 619 if( ot_sockets_tcp4_count == OT_MAXSOCKETS_TCP4 ) {
620 fprintf( stderr, "Too many tcp4 sockets, increase OT_MAXSOCKETS_TCP4 and recompile.\n"); exit(1);
621 }
622 if( socket_bind4_reuse( s, ip, port ) == -1 )
623 panic( "socket_bind4_reuse" );
624
625 if( socket_listen( s, SOMAXCONN) == -1 )
626 panic( "socket_listen" );
627
628 if( !io_fd( s ) )
629 panic( "io_fd" );
630
631 io_wantread( s );
632
633 ot_sockets_tcp4[ ot_sockets_tcp4_count++ ] = s;
634}
635
636void ot_try_bind_udp4( char ip[4], uint16 port ) {
637 int64 s = socket_udp4( );
638 if( ot_sockets_udp4_count == OT_MAXSOCKETS_UDP4 ) {
639 fprintf( stderr, "Too many udp4 sockets, increase OT_MAXSOCKETS_UDP4 and recompile.\n"); exit(1);
640 }
641 if( socket_bind4_reuse( s, ip, port ) == -1 )
642 panic( "socket_bind4_reuse" );
643
644 if( !io_fd( s ) )
645 panic( "io_fd" );
646
647 io_wantread( s );
648
649 ot_sockets_udp4[ ot_sockets_udp4_count++ ] = s;
650}
651
652int main( int argc, char **argv ) {
653 char serverip[4] = {0,0,0,0};
582 char *serverdir = "."; 654 char *serverdir = ".";
583 uint16 port = 6969;
584 int scanon = 1; 655 int scanon = 1;
585 656
586 while( scanon ) { 657 while( scanon ) {
587 switch( getopt( argc, argv, ":i:p:d:ocbBh" ) ) { 658 switch( getopt( argc, argv, ":i:p:d:ocbBh" ) ) {
588 case -1 : scanon = 0; break; 659 case -1 : scanon = 0; break;
589 case 'i': serverip = optarg; break; 660 case 'i': scan_ip4( optarg, serverip ); break;
590 case 'p': port = (uint16)atol( optarg ); break; 661 case 'p': ot_try_bind_tcp4( serverip, (uint16)atol( optarg ) ); break;
662 case 'P': ot_try_bind_udp4( serverip, (uint16)atol( optarg ) ); break;
591 case 'd': serverdir = optarg; break; 663 case 'd': serverdir = optarg; break;
592 case 'h': help( argv[0] ); exit( 0 ); 664 case 'h': help( argv[0] ); exit( 0 );
593#ifdef WANT_CLOSED_TRACKER 665#ifdef WANT_CLOSED_TRACKER
@@ -603,18 +675,13 @@ int main( int argc, char **argv ) {
603 } 675 }
604 } 676 }
605 677
606 if( socket_bind4_reuse( s, serverip, port ) == -1 ) 678 // Bind to our default tcp port
607 panic( "socket_bind4_reuse" ); 679 if( !ot_sockets_tcp4_count && !ot_sockets_udp4_count )
680 ot_try_bind_tcp4( serverip, 6969 );
608 681
609 setegid( (gid_t)-2 ); setuid( (uid_t)-2 ); 682 setegid( (gid_t)-2 ); setuid( (uid_t)-2 );
610 setgid( (gid_t)-2 ); seteuid( (uid_t)-2 ); 683 setgid( (gid_t)-2 ); seteuid( (uid_t)-2 );
611 684
612 if( socket_listen( s, SOMAXCONN) == -1 )
613 panic( "socket_listen" );
614
615 if( !io_fd( s ) )
616 panic( "io_fd" );
617
618 signal( SIGPIPE, SIG_IGN ); 685 signal( SIGPIPE, SIG_IGN );
619 signal( SIGINT, graceful ); 686 signal( SIGINT, graceful );
620 if( init_logic( serverdir ) == -1 ) 687 if( init_logic( serverdir ) == -1 )
@@ -622,7 +689,7 @@ int main( int argc, char **argv ) {
622 689
623 ot_start_time = time( NULL ); 690 ot_start_time = time( NULL );
624 691
625 server_mainloop( s ); 692 server_mainloop( );
626 693
627 return 0; 694 return 0;
628} 695}
diff --git a/trackerlogic.c b/trackerlogic.c
index 3b83b77..18e8cf5 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -221,6 +221,10 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
221 } else 221 } else
222 clean_peerlist( NOW, torrent->peer_list ); 222 clean_peerlist( NOW, torrent->peer_list );
223 223
224 /* Sanitize flags: Whoever claims to have completed download, must be a seeder */
225 if( ( OT_FLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED )
226 OT_FLAG( peer ) ^= PEER_FLAG_COMPLETED;
227
224 peer_pool = &torrent->peer_list->peers[0]; 228 peer_pool = &torrent->peer_list->peers[0];
225 peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch ); 229 peer_dest = vector_find_or_insert( peer_pool, (void*)peer, sizeof( ot_peer ), OT_PEER_COMPARE_SIZE, &exactmatch );
226 230