Browse Source

Code cleanup reindenting

dynamic-accesslists
erdgeist 18 years ago
parent
commit
2f0658a3af
  1. 170
      opentracker.c

170
opentracker.c

@ -44,33 +44,33 @@ static char fd_debug_space[0x10000];
static char debug_request[8192]; static char debug_request[8192];
#endif #endif
static void carp(const char* routine) { static void carp(const char* routine ) {
buffer_puts(buffer_2,routine); buffer_puts( buffer_2, routine );
buffer_puts(buffer_2,": "); buffer_puts( buffer_2, ": " );
buffer_puterror(buffer_2); buffer_puterror( buffer_2 );
buffer_putnlflush(buffer_2); buffer_putnlflush( buffer_2 );
} }
static void panic(const char* routine) { static void panic( const char* routine ) {
carp(routine); carp( routine );
exit(111); exit( 111 );
} }
struct http_data { struct http_data {
union { union {
array r; array request;
io_batch batch; io_batch batch;
}; };
unsigned char ip[4]; unsigned char ip[4];
}; };
int header_complete(struct http_data* r) { int header_complete( struct http_data* h ) {
int l = array_bytes(&r->r), i; int l = array_bytes( &h->request ), i;
const char* c = array_start(&r->r); const char* c = array_start( &h->request );
for (i=0; i+1<l; ++i) { for( i=0; i+1<l; ++i) {
if (c[i]=='\n' && c[i+1]=='\n') return i+2; 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; if( i+3<l && c[i]=='\r' && c[i+1]=='\n' && c[i+2]=='\r' && c[i+3]=='\n' ) return i+4;
} }
return 0; return 0;
} }
@ -81,7 +81,7 @@ void sendmallocdata( int64 s, struct http_data *h, char * buffer, size_t size )
size_t header_size; size_t header_size;
if( !h ) { free( buffer); return; } if( !h ) { free( buffer); return; }
array_reset(&h->r); array_reset( &h->request );
header = malloc( SUCCESS_HTTP_HEADER_LENGTH ); header = malloc( SUCCESS_HTTP_HEADER_LENGTH );
if( !header ) { free( buffer ); return; } 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 ); 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 ) { void senddata(int64 s, struct http_data* h, char *buffer, size_t size ) {
size_t written_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 ); written_size = write( s, buffer, size );
if( ( written_size < 0 ) || ( written_size == size ) ) { if( ( written_size < 0 ) || ( written_size == size ) ) {
#ifdef _DEBUG_FDS #ifdef _DEBUG_FDS
if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" ); if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" );
fd_debug_space[s] = 0; fd_debug_space[s] = 0;
#endif #endif
free(h); io_close( s ); free( h ); io_close( s );
} else { } else {
char * outbuf = malloc( size - written_size ); char * outbuf = malloc( size - written_size );
tai6464 t; 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 ); iob_addbuf_free( &h->batch, outbuf, size - written_size );
// writeable sockets just have a tcp timeout // 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_dontwantread( s );
io_wantwrite( 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); senddata(s,h,static_scratch,reply_size);
} }
const char* http_header(struct http_data* r,const char* h) { void httpresponse( int64 s, struct http_data* 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)
{
char *c, *data; char *c, *data;
ot_peer peer; ot_peer peer;
ot_torrent *torrent; ot_torrent *torrent;
@ -169,29 +154,29 @@ void httpresponse(int64 s,struct http_data* h)
unsigned short port = htons(6881); unsigned short port = htons(6881);
size_t reply_size = 0; size_t reply_size = 0;
array_cat0(&h->r); array_cat0( &h->request );
c = array_start(&h->r); c = array_start( &h->request );
#ifdef _DEBUG_HTTPERROR #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 #endif
if (byte_diff(c,4,"GET ")) { if( byte_diff( c, 4, "GET ") ) {
e400: 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; 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; if( *data != ' ' ) goto e400;
*data=0; *data = 0;
if (c[0]!='/') goto e404; if( c[0] != '/' ) goto e404;
while (*c=='/') ++c; while( *c == '/' ) ++c;
switch( scan_urlencoded_query( &c, data = c, SCAN_PATH ) ) { switch( scan_urlencoded_query( &c, data = c, SCAN_PATH ) ) {
case 5: /* scrape ? */ case 5: /* stats ? */
if (byte_diff(data,5,"stats")) if( byte_diff(data,5,"stats"))
goto e404; goto e404;
scanon = 1; scanon = 1;
mode = STATS_MRTG; mode = STATS_MRTG;
@ -204,7 +189,7 @@ e400:
case -1: /* error */ case -1: /* error */
goto e404; goto e404;
case 4: case 4:
if(byte_diff(data,4,"mode")) { if( byte_diff(data,4,"mode")) {
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
continue; continue;
} }
@ -227,7 +212,7 @@ e400:
goto e500; goto e500;
break; break;
case 6: /* scrape ? */ case 6: /* scrape ? */
if (byte_diff(data,6,"scrape")) if( byte_diff( data, 6, "scrape") )
goto e404; goto e404;
scanon = 1; scanon = 1;
@ -294,7 +279,7 @@ e400_param:
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
unsigned char ip[4]; unsigned char ip[4];
if( ( len <= 0 ) || scan_fixed_ip( data, len, ip ) ) goto e400_param; if( ( len <= 0 ) || scan_fixed_ip( data, len, ip ) ) goto e400_param;
OT_SETIP ( &peer, ip ); OT_SETIP( &peer, ip );
} else } else
scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
break; break;
@ -303,7 +288,7 @@ e400_param:
if(!byte_diff(data,4,"port")) { if(!byte_diff(data,4,"port")) {
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 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; 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")) { } else if(!byte_diff(data,4,"left")) {
size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
if( len <= 0 ) goto e400_param; if( len <= 0 ) goto e400_param;
@ -386,7 +371,7 @@ e500:
break; break;
default: /* neither *scrape nor announce */ default: /* neither *scrape nor announce */
e404: 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 ) { if( reply_size ) {
@ -408,7 +393,8 @@ e404:
senddata( s, h, static_scratch + reply_off, reply_size ); senddata( s, h, static_scratch + reply_off, reply_size );
} else { } else {
if( h ) array_reset(&h->r); if( h )
array_reset( &h->request );
#ifdef _DEBUG_FDS #ifdef _DEBUG_FDS
if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" ); if( !fd_debug_space[s] ) fprintf( stderr, "close on non-open fd\n" );
fd_debug_space[s] = 0; fd_debug_space[s] = 0;
@ -476,33 +462,33 @@ void handle_read( int64 clientsocket ) {
if( l <= 0 ) { if( l <= 0 ) {
if( h ) { if( h ) {
array_reset(&h->r); array_reset( &h->request );
free(h); free( h );
} }
#ifdef _DEBUG_FDS #ifdef _DEBUG_FDS
if( !fd_debug_space[clientsocket] ) fprintf( stderr, "close on non-open fd\n" ); if( !fd_debug_space[clientsocket] ) fprintf( stderr, "close on non-open fd\n" );
fd_debug_space[clientsocket] = 0; fd_debug_space[clientsocket] = 0;
#endif #endif
io_close(clientsocket); io_close( clientsocket );
return; return;
} }
array_catb(&h->r,static_scratch,l); array_catb( &h->request, static_scratch, l );
#ifdef _DEBUG_HTTPERROR #ifdef _DEBUG_HTTPERROR
memcpy( debug_request, "500!\0", 5 ); memcpy( debug_request, "500!\0", 5 );
#endif #endif
if( array_failed(&h->r)) if( array_failed( &h->request ) )
httperror(clientsocket,h,"500 Server Error","Request too long."); httperror( clientsocket, h, "500 Server Error", "Request too long.");
else if (array_bytes(&h->r)>8192) else if( array_bytes( &h->request ) > 8192 )
httperror(clientsocket,h,"500 request too long","You sent too much headers"); httperror( clientsocket, h, "500 request too long", "You sent too much headers");
else if ((l=header_complete(h))) else if( ( l = header_complete( h ) ) )
httpresponse(clientsocket,h); httpresponse( clientsocket, h);
} }
void handle_write( int64 clientsocket ) { void handle_write( int64 clientsocket ) {
struct http_data* h=io_getcookie(clientsocket); struct http_data* h=io_getcookie( clientsocket );
if( !h ) return; if( !h ) return;
if( iob_send( clientsocket, &h->batch ) <= 0 ) { if( iob_send( clientsocket, &h->batch ) <= 0 ) {
iob_reset( &h->batch ); iob_reset( &h->batch );
@ -525,7 +511,7 @@ void handle_accept( int64 serversocket ) {
while( ( i = socket_accept4( serversocket, (char*)ip, &port) ) != -1 ) { while( ( i = socket_accept4( serversocket, (char*)ip, &port) ) != -1 ) {
if( !io_fd( i ) || 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 ); io_close( i );
continue; continue;
} }
@ -557,9 +543,9 @@ void handle_accept( int64 serversocket ) {
void handle_timeouted( ) { void handle_timeouted( ) {
int64 i; int64 i;
while( ( i = io_timeouted() ) != -1 ) { while( ( i = io_timeouted() ) != -1 ) {
struct http_data* h=io_getcookie(i); struct http_data* h=io_getcookie( i );
if( h ) { if( h ) {
array_reset( &h->r ); array_reset( &h->request );
free( h ); free( h );
} }
#ifdef _DEBUG_FDS #ifdef _DEBUG_FDS
@ -576,28 +562,28 @@ void server_mainloop( int64 serversocket ) {
io_wantread( serversocket ); io_wantread( serversocket );
taia_now( &next_timeout_check ); taia_now( &next_timeout_check );
for (;;) { for( ; ; ) {
int64 i; int64 i;
taia_now(&t); taia_now( &t );
taia_addsec(&t,&t,OT_CLIENT_TIMEOUT_CHECKINTERVAL); taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_CHECKINTERVAL );
io_waituntil(t); io_waituntil( t );
while( ( i = io_canread() ) != -1 ) { while( ( i = io_canread( ) ) != -1 ) {
if( i == serversocket ) if( i == serversocket )
handle_accept( i ); handle_accept( i );
else else
handle_read( i ); handle_read( i );
} }
while( ( i = io_canwrite() ) != -1 ) while( ( i = io_canwrite( ) ) != -1 )
handle_write( i ); handle_write( i );
taia_now(&t); taia_now( &t );
if( taia_less( &next_timeout_check, &t ) ) { if( taia_less( &next_timeout_check, &t ) ) {
handle_timeouted( ); handle_timeouted( );
taia_now(&next_timeout_check); taia_now( &next_timeout_check );
taia_addsec(&next_timeout_check,&next_timeout_check,OT_CLIENT_TIMEOUT_CHECKINTERVAL); 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; int scanon = 1;
while( scanon ) { while( scanon ) {
switch( getopt(argc,argv,":i:p:d:ocbBh") ) { switch( getopt( argc, argv, ":i:p:d:ocbBh" ) ) {
case -1 : scanon = 0; break; case -1 : scanon = 0; break;
case 'i': serverip = optarg; break; case 'i': serverip = optarg; break;
case 'p': port = (uint16)atol( optarg ); break; case 'p': port = (uint16)atol( optarg ); break;
case 'd': serverdir = 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 #ifdef WANT_CLOSED_TRACKER
case 'o': g_closedtracker = 0; break; case 'o': g_closedtracker = 0; break;
case 'c': g_closedtracker = 1; break; case 'c': g_closedtracker = 1; break;
@ -625,21 +611,21 @@ int main( int argc, char **argv ) {
case 'B': g_check_blacklist = 0; break; case 'B': g_check_blacklist = 0; break;
#endif #endif
default: default:
case '?': usage( argv[0] ); exit(1); case '?': usage( argv[0] ); exit( 1 );
} }
} }
if (socket_bind4_reuse(s,serverip,port)==-1) if( socket_bind4_reuse( s, serverip, port ) == -1 )
panic("socket_bind4_reuse"); panic( "socket_bind4_reuse" );
setegid((gid_t)-2); setuid((uid_t)-2); setegid( (gid_t)-2 ); setuid( (uid_t)-2 );
setgid((gid_t)-2); seteuid((uid_t)-2); setgid( (gid_t)-2 ); seteuid( (uid_t)-2 );
if (socket_listen(s,SOMAXCONN)==-1) if( socket_listen( s, SOMAXCONN) == -1 )
panic("socket_listen"); panic( "socket_listen" );
if (!io_fd(s)) if( !io_fd( s ) )
panic("io_fd"); panic( "io_fd" );
signal( SIGPIPE, SIG_IGN ); signal( SIGPIPE, SIG_IGN );
signal( SIGINT, graceful ); signal( SIGINT, graceful );
@ -647,11 +633,11 @@ int main( int argc, char **argv ) {
signal( SIGINFO, count_fds ); signal( SIGINFO, count_fds );
#endif #endif
if( init_logic( serverdir ) == -1 ) if( init_logic( serverdir ) == -1 )
panic("Logic not started"); panic( "Logic not started" );
ot_start_time = time( NULL ); ot_start_time = time( NULL );
server_mainloop(s); server_mainloop( s );
return 0; return 0;
} }

Loading…
Cancel
Save