2007-01-26 16:26:49 +00:00
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2007-01-05 00:00:41 +00:00
It is considered beerware . Prost . Skol . Cheers or whatever .
Some of the stuff below is stolen from Fefes example libowfat httpd .
*/
2006-12-05 12:56:56 +00:00
# include "socket.h"
# include "io.h"
2007-01-20 01:50:28 +00:00
# include "iob.h"
2006-12-05 12:56:56 +00:00
# include "buffer.h"
# include "array.h"
2007-01-11 01:06:10 +00:00
# include "byte.h"
2006-12-05 12:56:56 +00:00
# include "case.h"
# include "fmt.h"
# include "str.h"
2007-03-05 21:14:36 +00:00
# include "scan.h"
# include "ip4.h"
2006-12-06 18:36:14 +00:00
# include <string.h>
2006-12-05 12:56:56 +00:00
# include <sys/types.h>
# include <sys/stat.h>
2006-12-16 16:14:34 +00:00
# include <sys/socket.h>
2007-10-19 03:41:23 +00:00
# include <sys/mman.h>
2006-12-15 23:29:38 +00:00
# include <arpa/inet.h>
2006-12-05 12:56:56 +00:00
# include <unistd.h>
# include <stdlib.h>
# include <errno.h>
2006-12-08 22:37:44 +00:00
# include <signal.h>
# include <stdio.h>
2007-04-02 17:26:40 +00:00
# include <pwd.h>
2006-12-05 12:56:56 +00:00
2006-12-08 20:28:17 +00:00
# include "trackerlogic.h"
# include "scan_urlencoded_query.h"
2007-11-06 11:58:12 +00:00
# include "ot_stats.h"
# include "ot_sync.h"
2006-12-08 20:28:17 +00:00
2007-01-26 16:26:49 +00:00
/* Globals */
2007-05-04 23:08:38 +00:00
static unsigned long long ot_overall_tcp_connections = 0 ;
static unsigned long long ot_overall_udp_connections = 0 ;
static unsigned long long ot_overall_tcp_successfulannounces = 0 ;
static unsigned long long ot_overall_udp_successfulannounces = 0 ;
2007-10-31 15:39:41 +00:00
static unsigned long long ot_full_scrape_count = 0 ;
static unsigned long long ot_full_scrape_size = 0 ;
2006-12-15 22:40:33 +00:00
static time_t ot_start_time ;
2007-01-11 01:06:10 +00:00
static const size_t SUCCESS_HTTP_HEADER_LENGTH = 80 ;
static const size_t SUCCESS_HTTP_SIZE_OFF = 17 ;
2007-11-01 20:13:03 +00:00
static uint32_t g_adminip_addresses [ OT_ADMINIP_MAX ] ;
static unsigned int g_adminip_count = 0 ;
2007-10-13 17:40:37 +00:00
time_t g_now ;
2007-08-18 09:56:22 +00:00
# if defined ( WANT_BLACKLISTING ) && defined (WANT_CLOSED_TRACKER )
# error WANT_BLACKLISTING and WANT_CLOSED_TRACKER are exclusive.
# endif
# if defined ( WANT_BLACKLISTING ) || defined (WANT_CLOSED_TRACKER )
static char * accesslist_filename = NULL ;
# define WANT_ACCESS_CONTROL
2007-07-22 00:40:10 +00:00
# endif
2007-02-01 13:30:58 +00:00
2007-10-29 17:22:05 +00:00
# ifndef WANT_TRACKER_SYNC
# define add_peer_to_torrent(A,B,C) add_peer_to_torrent(A,B)
# endif
2007-10-16 18:23:36 +00:00
# ifndef NO_FULLSCRAPE_LOGGING
2007-10-15 18:01:38 +00:00
# define LOG_TO_STDERR( ... ) fprintf( stderr, __VA_ARGS__ )
2007-10-16 18:23:36 +00:00
# else
# define LOG_TO_STDERR( ... )
# endif
2007-10-15 18:01:38 +00:00
2007-01-11 01:06:10 +00:00
/* To always have space for error messages ;) */
2007-02-01 13:30:58 +00:00
2007-01-29 02:02:03 +00:00
static char static_inbuf [ 8192 ] ;
2007-02-03 16:19:59 +00:00
static char static_outbuf [ 8192 ] ;
2006-12-15 22:07:33 +00:00
2007-10-19 21:56:59 +00:00
# define OT_MAXMULTISCRAPE_COUNT 64
static ot_hash multiscrape_buf [ OT_MAXMULTISCRAPE_COUNT ] ;
2007-03-07 22:19:00 +00:00
static char * FLAG_TCP = " TCP " ;
static char * FLAG_UDP = " UDP " ;
static size_t ot_sockets_count = 0 ;
2007-03-05 21:14:36 +00:00
2007-01-24 20:13:30 +00:00
# ifdef _DEBUG_HTTPERROR
static char debug_request [ 8192 ] ;
2007-11-02 13:13:03 +00:00
# define _DEBUG_HTTPERROR_PARAM( param ) , param
# else
# define _DEBUG_HTTPERROR_PARAM( param )
2007-01-24 20:13:30 +00:00
# endif
2007-01-19 17:50:35 +00:00
2007-10-19 01:26:33 +00:00
typedef enum {
STRUCT_HTTP_FLAG_ARRAY_USED = 1 ,
STRUCT_HTTP_FLAG_IOB_USED = 2
} STRUCT_HTTP_FLAG ;
2007-01-26 16:26:49 +00:00
struct http_data {
union {
2007-10-19 01:26:33 +00:00
array request ;
io_batch batch ;
2007-01-26 16:26:49 +00:00
} ;
2007-10-19 01:26:33 +00:00
unsigned char ip [ 4 ] ;
STRUCT_HTTP_FLAG flag ;
2007-01-26 16:26:49 +00:00
} ;
2007-11-01 20:13:03 +00:00
# define NOTBLESSED( h ) (!bsearch( &h->ip, g_adminip_addresses, g_adminip_count, 4, ot_ip_compare ))
static int ot_ip_compare ( const void * a , const void * b ) { return memcmp ( a , b , 4 ) ; }
2007-01-26 16:26:49 +00:00
/* Prototypes */
int main ( int argc , char * * argv ) ;
2007-01-29 02:02:03 +00:00
static void httperror ( const int64 s , const char * title , const char * message ) ;
2007-11-02 13:13:03 +00:00
static void httpresponse ( const int64 s , char * data _DEBUG_HTTPERROR_PARAM ( size_t l ) ) ;
2007-01-26 16:26:49 +00:00
2007-10-17 14:43:13 +00:00
static void sendmmapdata ( const int64 s , char * buffer , const size_t size ) ;
2007-01-29 02:02:03 +00:00
static void senddata ( const int64 s , char * buffer , const size_t size ) ;
2007-01-26 16:26:49 +00:00
2007-03-05 21:14:36 +00:00
static void server_mainloop ( ) ;
2007-01-26 16:26:49 +00:00
static void handle_timeouted ( void ) ;
static void handle_accept ( const int64 serversocket ) ;
static void handle_read ( const int64 clientsocket ) ;
static void handle_write ( const int64 clientsocket ) ;
2007-03-06 19:43:47 +00:00
static void handle_udp4 ( const int64 serversocket ) ;
2007-03-07 22:19:00 +00:00
static void ot_try_bind ( char ip [ 4 ] , uint16 port , int is_tcp ) ;
2007-01-26 16:26:49 +00:00
static void usage ( char * name ) ;
static void help ( char * name ) ;
static void carp ( const char * routine ) ;
static void panic ( const char * routine ) ;
2007-10-13 17:40:37 +00:00
static void signal_handler ( int s ) ;
2007-01-26 16:26:49 +00:00
2007-01-29 02:02:03 +00:00
# define HTTPERROR_400 return httperror( s, "400 Invalid Request", "This server only understands GET." )
# define HTTPERROR_400_PARAM return httperror( s, "400 Invalid Request", "Invalid parameter" )
# define HTTPERROR_400_COMPACT return httperror( s, "400 Invalid Request", "This server only delivers compact results." )
2007-03-27 16:09:03 +00:00
# define HTTPERROR_403_IP return httperror( s, "403 Access Denied", "Your ip address is not allowed to administrate this server." )
2007-01-29 02:02:03 +00:00
# define HTTPERROR_404 return httperror( s, "404 Not Found", "No such file or directory." )
# define HTTPERROR_500 return httperror( s, "500 Internal Server Error", "A server error has occured. Please retry later." )
2007-01-26 16:26:49 +00:00
/* End of prototypes */
static void carp ( const char * routine ) {
2007-01-24 22:23:18 +00:00
buffer_puts ( buffer_2 , routine ) ;
buffer_puts ( buffer_2 , " : " ) ;
buffer_puterror ( buffer_2 ) ;
buffer_putnlflush ( buffer_2 ) ;
2006-12-05 12:56:56 +00:00
}
2007-01-26 16:26:49 +00:00
static void panic ( const char * routine ) {
2007-01-24 22:23:18 +00:00
carp ( routine ) ;
exit ( 111 ) ;
2006-12-05 12:56:56 +00:00
}
2007-01-29 02:02:03 +00:00
static void httperror ( const int64 s , const char * title , const char * message ) {
size_t reply_size = sprintf ( static_outbuf , " HTTP/1.0 %s \r \n Content-Type: text/html \r \n Connection: close \r \n Content-Length: %zd \r \n \r \n <title>%s</title> \n " ,
2007-01-26 16:26:49 +00:00
title , strlen ( message ) + strlen ( title ) + 16 - 4 , title + 4 ) ;
# ifdef _DEBUG_HTTPERROR
fprintf ( stderr , " DEBUG: invalid request was: %s \n " , debug_request ) ;
# endif
2007-01-29 02:02:03 +00:00
senddata ( s , static_outbuf , reply_size ) ;
2007-01-26 16:26:49 +00:00
}
2007-10-17 14:43:13 +00:00
static void sendmmapdata ( const int64 s , char * buffer , size_t size ) {
2007-01-29 02:02:03 +00:00
struct http_data * h = io_getcookie ( s ) ;
2007-01-20 11:13:30 +00:00
char * header ;
size_t header_size ;
2007-01-29 02:02:03 +00:00
tai6464 t ;
2007-01-20 11:13:30 +00:00
2007-10-19 03:39:04 +00:00
if ( ! h ) {
munmap ( buffer , size ) ;
return ;
}
2007-10-19 01:26:33 +00:00
if ( h - > flag & STRUCT_HTTP_FLAG_ARRAY_USED ) {
h - > flag & = ~ STRUCT_HTTP_FLAG_ARRAY_USED ;
array_reset ( & h - > request ) ;
}
2007-01-20 11:13:30 +00:00
header = malloc ( SUCCESS_HTTP_HEADER_LENGTH ) ;
2007-01-26 16:26:49 +00:00
if ( ! header ) {
2007-10-19 03:39:04 +00:00
munmap ( buffer , size ) ;
2007-01-26 16:26:49 +00:00
HTTPERROR_500 ;
}
2007-01-20 11:13:30 +00:00
header_size = sprintf ( header , " HTTP/1.0 200 OK \r \n Content-Type: text/plain \r \n Content-Length: %zd \r \n \r \n " , size ) ;
iob_reset ( & h - > batch ) ;
iob_addbuf_free ( & h - > batch , header , header_size ) ;
2007-10-17 14:43:13 +00:00
iob_addbuf_munmap ( & h - > batch , buffer , size ) ;
2007-10-19 01:26:33 +00:00
h - > flag | = STRUCT_HTTP_FLAG_IOB_USED ;
2007-01-20 11:13:30 +00:00
2007-10-16 18:23:36 +00:00
/* writeable sockets timeout after twice the pool timeout
which defaults to 5 minutes ( e . g . after 10 minutes ) */
2007-10-19 02:00:53 +00:00
taia_now ( & t ) ; taia_addsec ( & t , & t , OT_CLIENT_TIMEOUT_SEND ) ;
io_timeout ( s , t ) ;
2007-01-20 11:13:30 +00:00
io_dontwantread ( s ) ;
io_wantwrite ( s ) ;
}
2007-01-29 02:02:03 +00:00
static void senddata ( const int64 s , char * buffer , size_t size ) {
struct http_data * h = io_getcookie ( s ) ;
2007-01-31 09:50:46 +00:00
ssize_t written_size ;
2007-01-08 00:34:37 +00:00
2007-01-24 22:23:18 +00:00
/* whoever sends data is not interested in its input-array */
2007-10-19 01:26:33 +00:00
if ( h & & ( h - > flag & STRUCT_HTTP_FLAG_ARRAY_USED ) ) {
h - > flag & = ~ STRUCT_HTTP_FLAG_ARRAY_USED ;
2007-01-24 22:23:18 +00:00
array_reset ( & h - > request ) ;
2007-10-19 01:26:33 +00:00
}
2007-01-24 22:23:18 +00:00
2007-01-09 06:30:37 +00:00
written_size = write ( s , buffer , size ) ;
2007-04-03 11:08:17 +00:00
if ( ( written_size < 0 ) | | ( ( size_t ) written_size = = size ) ) {
2007-01-24 22:23:18 +00:00
free ( h ) ; io_close ( s ) ;
2007-01-08 00:34:37 +00:00
} else {
2007-10-19 01:26:33 +00:00
char * outbuf ;
2007-01-20 01:50:28 +00:00
tai6464 t ;
2007-10-19 01:26:33 +00:00
if ( ! h ) return ;
if ( ! ( outbuf = malloc ( size - written_size ) ) ) {
2007-01-20 01:50:28 +00:00
free ( h ) ; io_close ( s ) ;
return ;
}
iob_reset ( & h - > batch ) ;
memmove ( outbuf , buffer + written_size , size - written_size ) ;
iob_addbuf_free ( & h - > batch , outbuf , size - written_size ) ;
2007-10-19 01:26:33 +00:00
h - > flag | = STRUCT_HTTP_FLAG_IOB_USED ;
2007-01-20 01:50:28 +00:00
2007-10-19 02:00:53 +00:00
/* writeable short data sockets just have a tcp timeout */
taia_uint ( & t , 0 ) ; io_timeout ( s , t ) ;
2007-01-20 11:13:30 +00:00
io_dontwantread ( s ) ;
io_wantwrite ( s ) ;
2007-01-08 00:34:37 +00:00
}
2006-12-05 12:56:56 +00:00
}
2007-11-02 13:13:03 +00:00
static void httpresponse ( const int64 s , char * data _DEBUG_HTTPERROR_PARAM ( size_t l ) ) {
2007-03-27 16:09:03 +00:00
struct http_data * h = io_getcookie ( s ) ;
2007-01-29 02:02:03 +00:00
char * c , * reply ;
2007-01-11 01:06:10 +00:00
ot_peer peer ;
ot_torrent * torrent ;
ot_hash * hash = NULL ;
2007-11-06 01:28:40 +00:00
int numwant , tmp , scanon , mode ;
2007-01-11 01:06:10 +00:00
unsigned short port = htons ( 6881 ) ;
2007-01-26 16:26:49 +00:00
time_t t ;
2007-01-31 09:50:46 +00:00
ssize_t len ;
2007-01-26 18:09:14 +00:00
size_t reply_size = 0 , reply_off ;
2007-01-11 01:06:10 +00:00
2007-01-24 20:13:30 +00:00
# ifdef _DEBUG_HTTPERROR
2007-10-21 03:37:26 +00:00
if ( l > = sizeof ( debug_request ) )
l = sizeof ( debug_request ) - 1 ;
memcpy ( debug_request , data , l ) ;
debug_request [ l ] = 0 ;
2007-01-24 20:13:30 +00:00
# endif
2007-01-29 02:02:03 +00:00
/* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */
if ( byte_diff ( data , 5 , " GET / " ) ) HTTPERROR_400 ;
2006-12-08 20:50:06 +00:00
2007-01-29 02:02:03 +00:00
/* Query string MUST terminate with SP -- we know that theres at least a '\n' where this search terminates */
for ( c = data + 5 ; * c ! = ' ' & & * c ! = ' \t ' & & * c ! = ' \n ' & & * c ! = ' \r ' ; + + c ) ;
if ( * c ! = ' ' ) HTTPERROR_400 ;
2007-01-11 01:06:10 +00:00
2007-01-29 02:02:03 +00:00
/* Skip leading '/' */
for ( c = data + 4 ; * c = = ' / ' ; + + c ) ;
2007-01-11 01:06:10 +00:00
switch ( scan_urlencoded_query ( & c , data = c , SCAN_PATH ) ) {
2007-10-27 14:06:07 +00:00
# ifdef WANT_TRACKER_SYNC
2007-03-27 16:09:03 +00:00
/******************************
* S Y N C *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-01-26 16:26:49 +00:00
case 4 : /* sync ? */
2007-10-21 05:36:10 +00:00
if ( * data = = ' a ' ) goto ANNOUNCE_WORKAROUND ;
2007-10-21 04:55:19 +00:00
if ( ! byte_diff ( data , 2 , " sc " ) ) goto SCRAPE_WORKAROUND ;
2007-01-26 16:26:49 +00:00
if ( byte_diff ( data , 4 , " sync " ) ) HTTPERROR_404 ;
2007-10-19 01:26:33 +00:00
if ( NOTBLESSED ( h ) ) HTTPERROR_403_IP ;
2007-03-27 16:09:03 +00:00
2007-10-15 18:01:38 +00:00
LOG_TO_STDERR ( " sync: %d.%d.%d.%d \n " , h - > ip [ 0 ] , h - > ip [ 1 ] , h - > ip [ 2 ] , h - > ip [ 3 ] ) ;
2007-03-27 16:09:03 +00:00
mode = SYNC_OUT ;
scanon = 1 ;
2007-03-27 12:07:29 +00:00
2007-03-27 16:09:03 +00:00
while ( scanon ) {
switch ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_PARAM ) ) {
case - 2 : scanon = 0 ; break ; /* TERMINATOR */
case - 1 : HTTPERROR_400_PARAM ; /* PARSE ERROR */
2007-10-18 23:33:07 +00:00
default : scan_urlencoded_skipvalue ( & c ) ; break ;
2007-03-27 16:09:03 +00:00
case 9 :
if ( byte_diff ( data , 9 , " changeset " ) ) {
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-03-27 16:09:03 +00:00
continue ;
}
/* ignore this, when we dont at least see "d4:syncdee" */
if ( ( len = scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ) < 10 ) HTTPERROR_400_PARAM ;
if ( add_changeset_to_tracker ( ( ot_byte * ) data , len ) ) HTTPERROR_400_PARAM ;
mode = SYNC_IN ;
break ;
}
}
if ( mode = = SYNC_OUT ) {
if ( ! ( reply_size = return_changeset_for_tracker ( & reply ) ) ) HTTPERROR_500 ;
2007-10-17 14:43:13 +00:00
return sendmmapdata ( s , reply , reply_size ) ;
2007-03-27 16:09:03 +00:00
}
/* Simple but proof for now */
reply = " OK " ;
reply_size = 2 ;
break ;
2007-10-27 14:06:07 +00:00
# endif
2007-03-27 16:09:03 +00:00
/******************************
* S T A T S *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-01-24 22:23:18 +00:00
case 5 : /* stats ? */
2007-10-21 05:36:10 +00:00
if ( * data = = ' a ' ) goto ANNOUNCE_WORKAROUND ;
2007-10-21 04:55:19 +00:00
if ( ! byte_diff ( data , 2 , " sc " ) ) goto SCRAPE_WORKAROUND ;
2007-01-26 16:26:49 +00:00
if ( byte_diff ( data , 5 , " stats " ) ) HTTPERROR_404 ;
2007-01-17 11:51:55 +00:00
scanon = 1 ;
2007-11-06 01:28:40 +00:00
mode = STATS_PEERS ;
2007-01-17 11:51:55 +00:00
while ( scanon ) {
switch ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_PARAM ) ) {
2007-01-26 16:26:49 +00:00
case - 2 : scanon = 0 ; break ; /* TERMINATOR */
case - 1 : HTTPERROR_400_PARAM ; /* PARSE ERROR */
2007-10-18 23:33:07 +00:00
default : scan_urlencoded_skipvalue ( & c ) ; break ;
2007-01-17 11:51:55 +00:00
case 4 :
2007-01-24 22:23:18 +00:00
if ( byte_diff ( data , 4 , " mode " ) ) {
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-17 11:51:55 +00:00
continue ;
}
2007-01-31 09:50:46 +00:00
if ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ! = 4 ) HTTPERROR_400_PARAM ;
2007-11-06 01:28:40 +00:00
if ( ! byte_diff ( data , 4 , " peer " ) )
mode = STATS_PEERS ;
else if ( ! byte_diff ( data , 4 , " conn " ) )
mode = STATS_CONNS ;
2007-01-17 11:51:55 +00:00
else if ( ! byte_diff ( data , 4 , " top5 " ) )
mode = STATS_TOP5 ;
2007-10-31 15:39:41 +00:00
else if ( ! byte_diff ( data , 4 , " fscr " ) )
mode = STATS_FULLSCRAPE ;
2007-02-01 13:51:39 +00:00
else if ( ! byte_diff ( data , 4 , " dmem " ) )
mode = STATS_DMEM ;
2007-03-15 23:14:14 +00:00
else if ( ! byte_diff ( data , 4 , " tcp4 " ) )
mode = STATS_TCP ;
else if ( ! byte_diff ( data , 4 , " udp4 " ) )
mode = STATS_UDP ;
2007-07-22 16:17:26 +00:00
else if ( ! byte_diff ( data , 4 , " s24s " ) )
mode = STATS_SLASH24S ;
2007-01-17 11:51:55 +00:00
else
2007-01-26 16:26:49 +00:00
HTTPERROR_400_PARAM ;
2007-01-17 11:51:55 +00:00
}
}
2007-11-06 01:28:40 +00:00
switch ( mode )
{
case STATS_DMEM :
if ( ! ( reply_size = return_memstat_for_tracker ( & reply ) ) ) HTTPERROR_500 ;
return sendmmapdata ( s , reply , reply_size ) ;
2007-03-15 23:14:14 +00:00
2007-11-06 01:28:40 +00:00
case STATS_CONNS :
t = time ( NULL ) - ot_start_time ;
reply_size = sprintf ( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH ,
" %llu \n %llu \n %i seconds (%i hours) \n opentracker - Pretuned by german engineers, currently handling %llu connections per second. " ,
ot_overall_tcp_connections + ot_overall_udp_connections , ot_overall_tcp_successfulannounces + ot_overall_udp_successfulannounces , ( int ) t , ( int ) ( t / 3600 ) , ( ot_overall_tcp_connections + ot_overall_udp_connections ) / ( ( unsigned int ) t ? ( unsigned int ) t : 1 ) ) ;
break ;
case STATS_UDP :
t = time ( NULL ) - ot_start_time ;
reply_size = sprintf ( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH ,
" %llu \n %llu \n %i seconds (%i hours) \n opentracker udp4 stats. " ,
ot_overall_udp_connections , ot_overall_udp_successfulannounces , ( int ) t , ( int ) ( t / 3600 ) ) ;
break ;
2007-03-15 23:14:14 +00:00
2007-11-06 01:28:40 +00:00
case STATS_TCP :
t = time ( NULL ) - ot_start_time ;
reply_size = sprintf ( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH ,
" %llu \n %llu \n %i seconds (%i hours) \n opentracker tcp4 stats. " ,
ot_overall_tcp_connections , ot_overall_tcp_successfulannounces , ( int ) t , ( int ) ( t / 3600 ) ) ;
break ;
2007-10-31 15:39:41 +00:00
2007-11-06 01:28:40 +00:00
default :
case STATS_PEERS :
/* Enough for http header + whole scrape string */
if ( ! ( reply_size = return_stats_for_tracker ( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf , mode ) ) ) HTTPERROR_500 ;
break ;
2007-10-31 15:39:41 +00:00
2007-11-06 01:28:40 +00:00
case STATS_FULLSCRAPE :
t = time ( NULL ) - ot_start_time ;
reply_size = sprintf ( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH ,
" %llu \n %llu \n %i seconds (%i hours) \n opentracker full scrape stats. " ,
ot_full_scrape_count * 1000 , ot_full_scrape_size , ( int ) t , ( int ) ( t / 3600 ) ) ;
break ;
2007-10-15 18:01:38 +00:00
2007-11-06 01:28:40 +00:00
case STATS_SLASH24S :
{
2007-09-26 16:49:13 +00:00
ot_dword diff ; struct timeval tv1 , tv2 ; gettimeofday ( & tv1 , NULL ) ;
if ( ! ( reply_size = return_stats_for_slash24s ( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf , 25 , 16 ) ) ) HTTPERROR_500 ;
gettimeofday ( & tv2 , NULL ) ; diff = ( tv2 . tv_sec - tv1 . tv_sec ) * 1000000 + tv2 . tv_usec - tv1 . tv_usec ;
2007-10-12 21:57:10 +00:00
reply_size + = sprintf ( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf + reply_size , " Time taken: %u \n " , diff ) ;
2007-11-06 01:28:40 +00:00
}
break ;
2007-03-15 23:14:14 +00:00
}
2007-01-16 02:57:32 +00:00
break ;
2007-03-27 16:09:03 +00:00
/******************************
* S C R A P E *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-01-11 01:06:10 +00:00
case 6 : /* scrape ? */
2007-10-21 05:36:10 +00:00
if ( * data = = ' a ' ) goto ANNOUNCE_WORKAROUND ;
2007-01-26 16:26:49 +00:00
if ( byte_diff ( data , 6 , " scrape " ) ) HTTPERROR_404 ;
2007-01-11 01:06:10 +00:00
2007-10-19 22:13:59 +00:00
/* Full scrape... you might want to limit that */
if ( ! byte_diff ( data , 12 , " scrape HTTP/ " ) ) {
2007-10-19 22:20:42 +00:00
LOG_TO_STDERR ( " [%08d] scrp: %d.%d.%d.%d - FULL SCRAPE \n " , ( unsigned int ) ( g_now - ot_start_time ) , h - > ip [ 0 ] , h - > ip [ 1 ] , h - > ip [ 2 ] , h - > ip [ 3 ] ) ;
2007-10-19 22:36:28 +00:00
# ifdef _DEBUG_HTTPERROR
write ( 2 , debug_request , l ) ;
# endif
2007-10-19 22:13:59 +00:00
if ( ! ( reply_size = return_fullscrape_for_tracker ( & reply ) ) ) HTTPERROR_500 ;
2007-10-31 15:39:41 +00:00
/* Stat keeping */
2007-10-19 22:13:59 +00:00
ot_overall_tcp_successfulannounces + + ;
2007-10-31 15:39:41 +00:00
ot_full_scrape_count + + ;
ot_full_scrape_size + = reply_size ;
2007-10-19 22:13:59 +00:00
return sendmmapdata ( s , reply , reply_size ) ;
}
2007-01-26 18:09:14 +00:00
SCRAPE_WORKAROUND :
2007-10-21 05:15:35 +00:00
/* This is to hack around stupid clients that send "scrape ?info_hash" */
2007-10-21 04:13:53 +00:00
if ( c [ - 1 ] ! = ' ? ' ) {
while ( ( * c ! = ' ? ' ) & & ( * c ! = ' \n ' ) ) + + c ;
if ( * c = = ' \n ' ) HTTPERROR_400_PARAM ;
2007-10-21 05:36:10 +00:00
+ + c ;
2007-10-21 04:13:53 +00:00
}
2007-01-26 18:09:14 +00:00
scanon = 1 ;
2007-11-06 01:28:40 +00:00
numwant = 0 ;
2007-01-11 01:06:10 +00:00
while ( scanon ) {
switch ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_PARAM ) ) {
2007-01-26 16:26:49 +00:00
case - 2 : scanon = 0 ; break ; /* TERMINATOR */
2007-10-21 05:15:35 +00:00
case - 1 :
2007-11-06 01:28:40 +00:00
if ( numwant )
2007-10-21 05:15:35 +00:00
goto UTORRENT1600_WORKAROUND ;
HTTPERROR_400_PARAM ; /* PARSE ERROR */
2007-10-18 23:33:07 +00:00
default : scan_urlencoded_skipvalue ( & c ) ; break ;
2007-01-11 01:06:10 +00:00
case 9 :
if ( byte_diff ( data , 9 , " info_hash " ) ) {
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-11 01:06:10 +00:00
continue ;
}
/* ignore this, when we have less than 20 bytes */
2007-10-23 00:01:10 +00:00
if ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ! = ( ssize_t ) sizeof ( ot_hash ) ) {
# ifdef WANT_UTORRENT1600_WORKAROUND
if ( data [ 20 ] ! = ' ? ' )
# endif
HTTPERROR_400_PARAM ;
}
2007-11-06 01:28:40 +00:00
if ( numwant < OT_MAXMULTISCRAPE_COUNT )
memmove ( multiscrape_buf + numwant + + , data , sizeof ( ot_hash ) ) ;
2007-01-11 01:06:10 +00:00
break ;
2006-12-14 02:44:50 +00:00
}
2007-01-11 01:06:10 +00:00
}
2006-12-14 02:44:50 +00:00
2007-10-23 00:01:10 +00:00
UTORRENT1600_WORKAROUND :
2007-10-19 22:13:59 +00:00
/* No info_hash found? Inform user */
2007-11-06 01:28:40 +00:00
if ( ! numwant ) HTTPERROR_400_PARAM ;
2007-01-26 16:26:49 +00:00
/* Enough for http header + whole scrape string */
2007-11-06 01:28:40 +00:00
if ( ! ( reply_size = return_tcp_scrape_for_torrent ( multiscrape_buf , numwant , SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) ) HTTPERROR_500 ;
2007-02-02 21:15:46 +00:00
2007-03-15 21:32:14 +00:00
ot_overall_tcp_successfulannounces + + ;
2007-01-11 01:06:10 +00:00
break ;
2007-03-27 16:09:03 +00:00
/******************************
* A N N O U N C E *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-01-27 16:06:13 +00:00
case 8 :
2007-10-21 04:55:19 +00:00
if ( ! byte_diff ( data , 2 , " sc " ) ) goto SCRAPE_WORKAROUND ;
2007-10-21 05:36:10 +00:00
if ( * data ! = ' a ' ) HTTPERROR_404 ;
2007-01-11 01:06:10 +00:00
2007-01-26 18:09:14 +00:00
ANNOUNCE_WORKAROUND :
2007-10-19 23:15:13 +00:00
/* This is to hack around stupid clients that send "announce ?info_hash" */
2007-10-21 04:13:53 +00:00
if ( c [ - 1 ] ! = ' ? ' ) {
while ( ( * c ! = ' ? ' ) & & ( * c ! = ' \n ' ) ) + + c ;
if ( * c = = ' \n ' ) HTTPERROR_400_PARAM ;
2007-10-21 05:36:10 +00:00
+ + c ;
2007-10-21 04:13:53 +00:00
}
2007-10-19 23:15:13 +00:00
2007-01-31 09:50:46 +00:00
OT_SETIP ( & peer , ( ( struct http_data * ) io_getcookie ( s ) ) - > ip ) ;
2007-01-11 01:06:10 +00:00
OT_SETPORT ( & peer , & port ) ;
OT_FLAG ( & peer ) = 0 ;
numwant = 50 ;
scanon = 1 ;
while ( scanon ) {
switch ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_PARAM ) ) {
2007-01-26 16:26:49 +00:00
case - 2 : scanon = 0 ; break ; /* TERMINATOR */
case - 1 : HTTPERROR_400_PARAM ; /* PARSE ERROR */
2007-10-18 23:33:07 +00:00
default : scan_urlencoded_skipvalue ( & c ) ; break ;
2007-01-03 05:11:48 +00:00
# ifdef WANT_IP_FROM_QUERY_STRING
2007-01-11 01:06:10 +00:00
case 2 :
if ( ! byte_diff ( data , 2 , " ip " ) ) {
unsigned char ip [ 4 ] ;
2007-01-31 09:50:46 +00:00
len = scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ;
2007-01-26 16:26:49 +00:00
if ( ( len < = 0 ) | | scan_fixed_ip ( data , len , ip ) ) HTTPERROR_400_PARAM ;
2007-01-24 22:23:18 +00:00
OT_SETIP ( & peer , ip ) ;
2007-01-11 01:06:10 +00:00
} else
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-11 01:06:10 +00:00
break ;
2007-01-03 05:11:48 +00:00
# endif
2007-01-11 01:06:10 +00:00
case 4 :
2007-01-31 09:50:46 +00:00
if ( ! byte_diff ( data , 4 , " port " ) ) {
len = scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ;
2007-01-26 16:26:49 +00:00
if ( ( len < = 0 ) | | scan_fixed_int ( data , len , & tmp ) | | ( tmp > 0xffff ) ) HTTPERROR_400_PARAM ;
2007-01-24 22:23:18 +00:00
port = htons ( tmp ) ; OT_SETPORT ( & peer , & port ) ;
2007-01-31 09:50:46 +00:00
} else if ( ! byte_diff ( data , 4 , " left " ) ) {
if ( ( len = scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ) < = 0 ) HTTPERROR_400_PARAM ;
2007-01-24 20:48:25 +00:00
if ( scan_fixed_int ( data , len , & tmp ) ) tmp = 0 ;
2007-01-11 01:06:10 +00:00
if ( ! tmp ) OT_FLAG ( & peer ) | = PEER_FLAG_SEEDING ;
} else
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-11 01:06:10 +00:00
break ;
case 5 :
2007-01-31 09:50:46 +00:00
if ( byte_diff ( data , 5 , " event " ) )
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-11 01:06:10 +00:00
else switch ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ) {
case - 1 :
2007-01-26 16:26:49 +00:00
HTTPERROR_400_PARAM ;
2006-12-08 21:00:56 +00:00
case 7 :
2007-01-31 09:50:46 +00:00
if ( ! byte_diff ( data , 7 , " stopped " ) ) OT_FLAG ( & peer ) | = PEER_FLAG_STOPPED ;
2006-12-08 21:36:25 +00:00
break ;
case 9 :
2007-01-31 09:50:46 +00:00
if ( ! byte_diff ( data , 9 , " completed " ) ) OT_FLAG ( & peer ) | = PEER_FLAG_COMPLETED ;
2007-01-11 01:06:10 +00:00
default : /* Fall through intended */
2007-01-08 00:34:37 +00:00
break ;
2007-01-11 01:06:10 +00:00
}
break ;
case 7 :
if ( ! byte_diff ( data , 7 , " numwant " ) ) {
2007-01-31 09:50:46 +00:00
len = scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ;
2007-01-26 16:26:49 +00:00
if ( ( len < = 0 ) | | scan_fixed_int ( data , len , & numwant ) ) HTTPERROR_400_PARAM ;
2007-10-23 00:39:15 +00:00
if ( numwant < 0 ) numwant = 50 ;
2007-01-11 01:06:10 +00:00
if ( numwant > 200 ) numwant = 200 ;
} else if ( ! byte_diff ( data , 7 , " compact " ) ) {
2007-01-31 09:50:46 +00:00
len = scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ;
2007-01-26 16:26:49 +00:00
if ( ( len < = 0 ) | | scan_fixed_int ( data , len , & tmp ) ) HTTPERROR_400_PARAM ;
if ( ! tmp ) HTTPERROR_400_COMPACT ;
2007-01-11 01:06:10 +00:00
} else
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-11 01:06:10 +00:00
break ;
case 9 :
if ( byte_diff ( data , 9 , " info_hash " ) ) {
2007-10-18 23:33:07 +00:00
scan_urlencoded_skipvalue ( & c ) ;
2007-01-11 01:06:10 +00:00
continue ;
2006-12-08 20:07:26 +00:00
}
2007-01-11 01:06:10 +00:00
/* ignore this, when we have less than 20 bytes */
2007-01-26 16:26:49 +00:00
if ( scan_urlencoded_query ( & c , data = c , SCAN_SEARCHPATH_VALUE ) ! = 20 ) HTTPERROR_400_PARAM ;
2007-01-11 01:06:10 +00:00
hash = ( ot_hash * ) data ;
break ;
2006-12-08 20:50:06 +00:00
}
2007-01-11 01:06:10 +00:00
}
2006-12-08 21:36:25 +00:00
2007-01-31 09:58:32 +00:00
/* Scanned whole query string */
if ( ! hash ) {
reply_size = sprintf ( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH , " d14:failure reason81:Your client forgot to send your torrent's info_hash. Please upgrade your client.e " ) ;
break ;
}
2007-09-10 02:43:10 +00:00
if ( OT_FLAG ( & peer ) & PEER_FLAG_STOPPED )
reply_size = remove_peer_from_torrent ( hash , & peer , SUCCESS_HTTP_HEADER_LENGTH + static_outbuf , 1 ) ;
else {
2007-03-27 16:09:03 +00:00
torrent = add_peer_to_torrent ( hash , & peer , 0 ) ;
2007-11-06 01:28:40 +00:00
if ( ! torrent | | ! ( reply_size = return_peers_for_torrent ( hash , numwant , SUCCESS_HTTP_HEADER_LENGTH + static_outbuf , 1 ) ) ) HTTPERROR_500 ;
2006-12-08 20:07:26 +00:00
}
2007-03-15 21:32:14 +00:00
ot_overall_tcp_successfulannounces + + ;
2007-01-11 01:06:10 +00:00
break ;
2007-10-21 04:55:19 +00:00
default :
2007-10-23 00:30:46 +00:00
if ( ( * data = = ' a ' ) | | ( * data = = ' ? ' ) ) goto ANNOUNCE_WORKAROUND ;
2007-10-21 04:55:19 +00:00
if ( ! byte_diff ( data , 2 , " sc " ) ) goto SCRAPE_WORKAROUND ;
2007-01-26 16:26:49 +00:00
HTTPERROR_404 ;
2007-01-11 01:06:10 +00:00
}
2007-01-31 09:50:46 +00:00
if ( ! reply_size ) HTTPERROR_500 ;
2007-01-11 01:06:10 +00:00
2007-01-26 16:26:49 +00:00
/* This one is rather ugly, so I take you step by step through it.
2007-01-11 01:06:10 +00:00
2007-01-26 16:26:49 +00:00
1. In order to avoid having two buffers , one for header and one for content , we allow all above functions from trackerlogic to
write to a fixed location , leaving SUCCESS_HTTP_HEADER_LENGTH bytes in our static buffer , which is enough for the static string
2007-03-07 22:19:00 +00:00
plus dynamic space needed to expand our Content - Length value . We reserve SUCCESS_HTTP_SIZE_OFF for its expansion and calculate
2007-01-26 16:26:49 +00:00
the space NOT needed to expand in reply_off
*/
2007-01-29 02:02:03 +00:00
reply_off = SUCCESS_HTTP_SIZE_OFF - snprintf ( static_outbuf , 0 , " %zd " , reply_size ) ;
2007-01-11 01:06:10 +00:00
2007-01-26 16:26:49 +00:00
/* 2. Now we sprintf our header so that sprintf writes its terminating '\0' exactly one byte before content starts. Complete
packet size is increased by size of header plus one byte ' \n ' , we will copy over ' \0 ' in next step */
2007-01-29 02:02:03 +00:00
reply_size + = 1 + sprintf ( static_outbuf + reply_off , " HTTP/1.0 200 OK \r \n Content-Type: text/plain \r \n Content-Length: %zd \r \n \r " , reply_size ) ;
2007-01-11 01:06:10 +00:00
2007-01-26 16:26:49 +00:00
/* 3. Finally we join both blocks neatly */