mirror of
git://erdgeist.org/opentracker
synced 2025-02-23 01:21:29 +08:00
Code cleanup reindenting
This commit is contained in:
parent
bcef9d56a4
commit
2f0658a3af
170
opentracker.c
170
opentracker.c
@ -44,33 +44,33 @@ static char fd_debug_space[0x10000];
|
||||
static char debug_request[8192];
|
||||
#endif
|
||||
|
||||
static void carp(const char* routine) {
|
||||
buffer_puts(buffer_2,routine);
|
||||
buffer_puts(buffer_2,": ");
|
||||
buffer_puterror(buffer_2);
|
||||
buffer_putnlflush(buffer_2);
|
||||
static void carp(const char* routine ) {
|
||||
buffer_puts( buffer_2, routine );
|
||||
buffer_puts( buffer_2, ": " );
|
||||
buffer_puterror( buffer_2 );
|
||||
buffer_putnlflush( buffer_2 );
|
||||
}
|
||||
|
||||
static void panic(const char* routine) {
|
||||
carp(routine);
|
||||
exit(111);
|
||||
static void panic( const char* routine ) {
|
||||
carp( routine );
|
||||
exit( 111 );
|
||||
}
|
||||
|
||||
struct http_data {
|
||||
union {
|
||||
array r;
|
||||
array request;
|
||||
io_batch batch;
|
||||
};
|
||||
unsigned char ip[4];
|
||||
};
|
||||
|
||||
int header_complete(struct http_data* r) {
|
||||
int l = array_bytes(&r->r), i;
|
||||
const char* c = array_start(&r->r);
|
||||
int header_complete( struct http_data* h ) {
|
||||
int l = array_bytes( &h->request ), i;
|
||||
const char* c = array_start( &h->request );
|
||||
|
||||
for (i=0; i+1<l; ++i) {
|
||||
if (c[i]=='\n' && c[i+1]=='\n') return i+2;
|
||||
if (i+3<l && c[i]=='\r' && c[i+1]=='\n' && c[i+2]=='\r' && c[i+3]=='\n') return i+4;
|
||||
for( i=0; i+1<l; ++i) {
|
||||
if( c[i]=='\n' && c[i+1]=='\n') return i+2;
|
||||
if( i+3<l && c[i]=='\r' && c[i+1]=='\n' && c[i+2]=='\r' && c[i+3]=='\n' ) return i+4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -81,7 +81,7 @@ void sendmallocdata( int64 s, struct http_data *h, char * buffer, size_t size )
|
||||
size_t header_size;
|
||||
|
||||
if( !h ) { free( buffer); return; }
|
||||
array_reset(&h->r);
|
||||
array_reset( &h->request );
|
||||
|
||||
header = malloc( SUCCESS_HTTP_HEADER_LENGTH );
|
||||
if( !header ) { free( buffer ); return; }
|
||||
@ -98,18 +98,20 @@ void sendmallocdata( int64 s, struct http_data *h, char * buffer, size_t size )
|
||||
io_wantwrite( s );
|
||||
}
|
||||
|
||||
/* whoever sends data is not interested in its input-array */
|
||||
void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) {
|
||||
size_t written_size;
|
||||
|
||||
if( h ) array_reset(&h->r);
|
||||
/* whoever sends data is not interested in its input-array */
|
||||
if( h )
|
||||
array_reset( &h->request );
|
||||
|
||||
written_size = write( s, buffer, size );
|
||||
if( ( written_size < 0 ) || ( written_size == size ) ) {
|
||||
#ifdef _DEBUG_FDS
|
||||
if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" );
|
||||
fd_debug_space[s] = 0;
|
||||
#endif
|
||||
free(h); io_close( s );
|
||||
free( h ); io_close( s );
|
||||
} else {
|
||||
char * outbuf = malloc( size - written_size );
|
||||
tai6464 t;
|
||||
@ -128,7 +130,7 @@ void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) {
|
||||
iob_addbuf_free( &h->batch, outbuf, size - written_size );
|
||||
|
||||
// writeable sockets just have a tcp timeout
|
||||
taia_uint(&t,0); io_timeout( s, t );
|
||||
taia_uint( &t, 0 ); io_timeout( s, t );
|
||||
io_dontwantread( s );
|
||||
io_wantwrite( s );
|
||||
}
|
||||
@ -143,24 +145,7 @@ void httperror(int64 s,struct http_data* h,const char* title,const char* message
|
||||
senddata(s,h,static_scratch,reply_size);
|
||||
}
|
||||
|
||||
const char* http_header(struct http_data* r,const char* h) {
|
||||
int i, l = array_bytes(&r->r);
|
||||
int sl = strlen(h);
|
||||
const char* c = array_start(&r->r);
|
||||
|
||||
for (i=0; i+sl+2<l; ++i) {
|
||||
if (c[i]=='\n' && case_equalb(c+i+1,sl,h) && c[i+sl+1]==':') {
|
||||
c+=i+sl+1;
|
||||
if (*c==' ' || *c=='\t') ++c;
|
||||
return c;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void httpresponse(int64 s,struct http_data* h)
|
||||
{
|
||||
void httpresponse( int64 s, struct http_data* h) {
|
||||
char *c, *data;
|
||||
ot_peer peer;
|
||||
ot_torrent *torrent;
|
||||
@ -169,29 +154,29 @@ void httpresponse(int64 s,struct http_data* h)
|
||||
unsigned short port = htons(6881);
|
||||
size_t reply_size = 0;
|
||||
|
||||
array_cat0(&h->r);
|
||||
c = array_start(&h->r);
|
||||
array_cat0( &h->request );
|
||||
c = array_start( &h->request );
|
||||
|
||||
#ifdef _DEBUG_HTTPERROR
|
||||
memcpy( debug_request, array_start(&h->r), array_bytes(&h->r) );
|
||||
memcpy( debug_request, array_start( &h->request ), array_bytes( &h->request ) );
|
||||
#endif
|
||||
|
||||
if (byte_diff(c,4,"GET ")) {
|
||||
if( byte_diff( c, 4, "GET ") ) {
|
||||
e400:
|
||||
return httperror(s,h,"400 Invalid Request","This server only understands GET.");
|
||||
return httperror( s, h, "400 Invalid Request", "This server only understands GET." );
|
||||
}
|
||||
|
||||
c+=4;
|
||||
for (data=c; *data!=' '&&*data!='\t'&&*data!='\n'&&*data!='\r'; ++data) ;
|
||||
for( data = c; *data!=' ' && *data != '\t' && *data != '\n' && *data != '\r'; ++data ) ;
|
||||
|
||||
if (*data!=' ') goto e400;
|
||||
*data=0;
|
||||
if (c[0]!='/') goto e404;
|
||||
while (*c=='/') ++c;
|
||||
if( *data != ' ' ) goto e400;
|
||||
*data = 0;
|
||||
if( c[0] != '/' ) goto e404;
|
||||
while( *c == '/' ) ++c;
|
||||
|
||||
switch( scan_urlencoded_query( &c, data = c, SCAN_PATH ) ) {
|
||||
case 5: /* scrape ? */
|
||||
if (byte_diff(data,5,"stats"))
|
||||
case 5: /* stats ? */
|
||||
if( byte_diff(data,5,"stats"))
|
||||
goto e404;
|
||||
scanon = 1;
|
||||
mode = STATS_MRTG;
|
||||
@ -204,7 +189,7 @@ e400:
|
||||
case -1: /* error */
|
||||
goto e404;
|
||||
case 4:
|
||||
if(byte_diff(data,4,"mode")) {
|
||||
if( byte_diff(data,4,"mode")) {
|
||||
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
|
||||
continue;
|
||||
}
|
||||
@ -227,7 +212,7 @@ e400:
|
||||
goto e500;
|
||||
break;
|
||||
case 6: /* scrape ? */
|
||||
if (byte_diff(data,6,"scrape"))
|
||||
if( byte_diff( data, 6, "scrape") )
|
||||
goto e404;
|
||||
scanon = 1;
|
||||
|
||||
@ -294,7 +279,7 @@ e400_param:
|
||||
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
|
||||
unsigned char ip[4];
|
||||
if( ( len <= 0 ) || scan_fixed_ip( data, len, ip ) ) goto e400_param;
|
||||
OT_SETIP ( &peer, ip );
|
||||
OT_SETIP( &peer, ip );
|
||||
} else
|
||||
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
|
||||
break;
|
||||
@ -303,7 +288,7 @@ e400_param:
|
||||
if(!byte_diff(data,4,"port")) {
|
||||
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
|
||||
if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || ( tmp > 0xffff ) ) goto e400_param;
|
||||
port = htons( tmp ); OT_SETPORT ( &peer, &port );
|
||||
port = htons( tmp ); OT_SETPORT( &peer, &port );
|
||||
} else if(!byte_diff(data,4,"left")) {
|
||||
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
|
||||
if( len <= 0 ) goto e400_param;
|
||||
@ -386,7 +371,7 @@ e500:
|
||||
break;
|
||||
default: /* neither *scrape nor announce */
|
||||
e404:
|
||||
return httperror(s,h,"404 Not Found","No such file or directory.");
|
||||
return httperror( s, h, "404 Not Found", "No such file or directory." );
|
||||
}
|
||||
|
||||
if( reply_size ) {
|
||||
@ -408,7 +393,8 @@ e404:
|
||||
|
||||
senddata( s, h, static_scratch + reply_off, reply_size );
|
||||
} else {
|
||||
if( h ) array_reset(&h->r);
|
||||
if( h )
|
||||
array_reset( &h->request );
|
||||
#ifdef _DEBUG_FDS
|
||||
if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" );
|
||||
fd_debug_space[s] = 0;
|
||||
@ -476,33 +462,33 @@ void handle_read( int64 clientsocket ) {
|
||||
|
||||
if( l <= 0 ) {
|
||||
if( h ) {
|
||||
array_reset(&h->r);
|
||||
free(h);
|
||||
array_reset( &h->request );
|
||||
free( h );
|
||||
}
|
||||
#ifdef _DEBUG_FDS
|
||||
if( !fd_debug_space[clientsocket] ) fprintf( stderr, "close on non-open fd\n" );
|
||||
fd_debug_space[clientsocket] = 0;
|
||||
#endif
|
||||
io_close(clientsocket);
|
||||
io_close( clientsocket );
|
||||
return;
|
||||
}
|
||||
|
||||
array_catb(&h->r,static_scratch,l);
|
||||
array_catb( &h->request, static_scratch, l );
|
||||
|
||||
#ifdef _DEBUG_HTTPERROR
|
||||
memcpy( debug_request, "500!\0", 5 );
|
||||
#endif
|
||||
|
||||
if( array_failed(&h->r))
|
||||
httperror(clientsocket,h,"500 Server Error","Request too long.");
|
||||
else if (array_bytes(&h->r)>8192)
|
||||
httperror(clientsocket,h,"500 request too long","You sent too much headers");
|
||||
else if ((l=header_complete(h)))
|
||||
httpresponse(clientsocket,h);
|
||||
if( array_failed( &h->request ) )
|
||||
httperror( clientsocket, h, "500 Server Error", "Request too long.");
|
||||
else if( array_bytes( &h->request ) > 8192 )
|
||||
httperror( clientsocket, h, "500 request too long", "You sent too much headers");
|
||||
else if( ( l = header_complete( h ) ) )
|
||||
httpresponse( clientsocket, h);
|
||||
}
|
||||
|
||||
void handle_write( int64 clientsocket ) {
|
||||
struct http_data* h=io_getcookie(clientsocket);
|
||||
struct http_data* h=io_getcookie( clientsocket );
|
||||
if( !h ) return;
|
||||
if( iob_send( clientsocket, &h->batch ) <= 0 ) {
|
||||
iob_reset( &h->batch );
|
||||
@ -525,7 +511,7 @@ void handle_accept( int64 serversocket ) {
|
||||
while( ( i = socket_accept4( serversocket, (char*)ip, &port) ) != -1 ) {
|
||||
|
||||
if( !io_fd( i ) ||
|
||||
!(h = (struct http_data*)malloc(sizeof(struct http_data))) ) {
|
||||
!( h = (struct http_data*)malloc( sizeof struct http_data ) ) ) {
|
||||
io_close( i );
|
||||
continue;
|
||||
}
|
||||
@ -557,9 +543,9 @@ void handle_accept( int64 serversocket ) {
|
||||
void handle_timeouted( ) {
|
||||
int64 i;
|
||||
while( ( i = io_timeouted() ) != -1 ) {
|
||||
struct http_data* h=io_getcookie(i);
|
||||
struct http_data* h=io_getcookie( i );
|
||||
if( h ) {
|
||||
array_reset( &h->r );
|
||||
array_reset( &h->request );
|
||||
free( h );
|
||||
}
|
||||
#ifdef _DEBUG_FDS
|
||||
@ -576,28 +562,28 @@ void server_mainloop( int64 serversocket ) {
|
||||
io_wantread( serversocket );
|
||||
taia_now( &next_timeout_check );
|
||||
|
||||
for (;;) {
|
||||
for( ; ; ) {
|
||||
int64 i;
|
||||
|
||||
taia_now(&t);
|
||||
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
|
||||
io_waituntil(t);
|
||||
taia_now( &t );
|
||||
taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_CHECKINTERVAL );
|
||||
io_waituntil( t );
|
||||
|
||||
while( ( i = io_canread() ) != -1 ) {
|
||||
while( ( i = io_canread( ) ) != -1 ) {
|
||||
if( i == serversocket )
|
||||
handle_accept( i );
|
||||
else
|
||||
handle_read( i );
|
||||
}
|
||||
|
||||
while( ( i = io_canwrite() ) != -1 )
|
||||
while( ( i = io_canwrite( ) ) != -1 )
|
||||
handle_write( i );
|
||||
|
||||
taia_now(&t);
|
||||
taia_now( &t );
|
||||
if( taia_less( &next_timeout_check, &t ) ) {
|
||||
handle_timeouted( );
|
||||
taia_now(&next_timeout_check);
|
||||
taia_addsec(&next_timeout_check,&next_timeout_check,OT_CLIENT_TIMEOUT_CHECKINTERVAL);
|
||||
taia_now( &next_timeout_check );
|
||||
taia_addsec( &next_timeout_check, &next_timeout_check, OT_CLIENT_TIMEOUT_CHECKINTERVAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -610,12 +596,12 @@ int main( int argc, char **argv ) {
|
||||
int scanon = 1;
|
||||
|
||||
while( scanon ) {
|
||||
switch( getopt(argc,argv,":i:p:d:ocbBh") ) {
|
||||
switch( getopt( argc, argv, ":i:p:d:ocbBh" ) ) {
|
||||
case -1 : scanon = 0; break;
|
||||
case 'i': serverip = optarg; break;
|
||||
case 'p': port = (uint16)atol( optarg ); break;
|
||||
case 'd': serverdir = optarg; break;
|
||||
case 'h': help( argv[0]); exit(0);
|
||||
case 'h': help( argv[0] ); exit( 0 );
|
||||
#ifdef WANT_CLOSED_TRACKER
|
||||
case 'o': g_closedtracker = 0; break;
|
||||
case 'c': g_closedtracker = 1; break;
|
||||
@ -625,21 +611,21 @@ int main( int argc, char **argv ) {
|
||||
case 'B': g_check_blacklist = 0; break;
|
||||
#endif
|
||||
default:
|
||||
case '?': usage( argv[0] ); exit(1);
|
||||
case '?': usage( argv[0] ); exit( 1 );
|
||||
}
|
||||
}
|
||||
|
||||
if (socket_bind4_reuse(s,serverip,port)==-1)
|
||||
panic("socket_bind4_reuse");
|
||||
if( socket_bind4_reuse( s, serverip, port ) == -1 )
|
||||
panic( "socket_bind4_reuse" );
|
||||
|
||||
setegid((gid_t)-2); setuid((uid_t)-2);
|
||||
setgid((gid_t)-2); seteuid((uid_t)-2);
|
||||
setegid( (gid_t)-2 ); setuid( (uid_t)-2 );
|
||||
setgid( (gid_t)-2 ); seteuid( (uid_t)-2 );
|
||||
|
||||
if (socket_listen(s,SOMAXCONN)==-1)
|
||||
panic("socket_listen");
|
||||
if( socket_listen( s, SOMAXCONN) == -1 )
|
||||
panic( "socket_listen" );
|
||||
|
||||
if (!io_fd(s))
|
||||
panic("io_fd");
|
||||
if( !io_fd( s ) )
|
||||
panic( "io_fd" );
|
||||
|
||||
signal( SIGPIPE, SIG_IGN );
|
||||
signal( SIGINT, graceful );
|
||||
@ -647,11 +633,11 @@ int main( int argc, char **argv ) {
|
||||
signal( SIGINFO, count_fds );
|
||||
#endif
|
||||
if( init_logic( serverdir ) == -1 )
|
||||
panic("Logic not started");
|
||||
panic( "Logic not started" );
|
||||
|
||||
ot_start_time = time( NULL );
|
||||
|
||||
server_mainloop(s);
|
||||
server_mainloop( s );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user