summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c53
1 files changed, 26 insertions, 27 deletions
diff --git a/opentracker.c b/opentracker.c
index 164cce8..21c32a2 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -130,7 +130,7 @@ static void sendmallocdata( const int64 s, char *buffer, size_t size ) {
130 130
131static void senddata( const int64 s, char *buffer, size_t size ) { 131static void senddata( const int64 s, char *buffer, size_t size ) {
132 struct http_data *h = io_getcookie( s ); 132 struct http_data *h = io_getcookie( s );
133 size_t written_size; 133 ssize_t written_size;
134 134
135 /* whoever sends data is not interested in its input-array */ 135 /* whoever sends data is not interested in its input-array */
136 if( h ) 136 if( h )
@@ -167,6 +167,7 @@ static void httpresponse( const int64 s, char *data ) {
167 int numwant, tmp, scanon, mode; 167 int numwant, tmp, scanon, mode;
168 unsigned short port = htons(6881); 168 unsigned short port = htons(6881);
169 time_t t; 169 time_t t;
170 ssize_t len;
170 size_t reply_size = 0, reply_off; 171 size_t reply_size = 0, reply_off;
171 172
172#ifdef _DEBUG_HTTPERROR 173#ifdef _DEBUG_HTTPERROR
@@ -208,7 +209,7 @@ static void httpresponse( const int64 s, char *data ) {
208 } 209 }
209 210
210 if( !hash ) HTTPERROR_400_PARAM; 211 if( !hash ) HTTPERROR_400_PARAM;
211 if( ( reply_size = return_sync_for_torrent( hash, &reply ) ) <= 0 ) HTTPERROR_500; 212 if( !( reply_size = return_sync_for_torrent( hash, &reply ) ) ) HTTPERROR_500;
212 213
213 return sendmallocdata( s, reply, reply_size ); 214 return sendmallocdata( s, reply, reply_size );
214 case 5: /* stats ? */ 215 case 5: /* stats ? */
@@ -226,8 +227,7 @@ static void httpresponse( const int64 s, char *data ) {
226 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); 227 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
227 continue; 228 continue;
228 } 229 }
229 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 230 if( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) != 4 ) HTTPERROR_400_PARAM;
230 if( len <= 0 ) HTTPERROR_400_PARAM;
231 if( !byte_diff(data,4,"mrtg")) 231 if( !byte_diff(data,4,"mrtg"))
232 mode = STATS_MRTG; 232 mode = STATS_MRTG;
233 else if( !byte_diff(data,4,"top5")) 233 else if( !byte_diff(data,4,"top5"))
@@ -238,7 +238,7 @@ static void httpresponse( const int64 s, char *data ) {
238 } 238 }
239 239
240 /* Enough for http header + whole scrape string */ 240 /* Enough for http header + whole scrape string */
241 if( ( reply_size = return_stats_for_tracker( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, mode ) ) <= 0 ) HTTPERROR_500; 241 if( !( reply_size = return_stats_for_tracker( SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, mode ) ) ) HTTPERROR_500;
242 242
243 break; 243 break;
244 case 6: /* scrape ? */ 244 case 6: /* scrape ? */
@@ -266,19 +266,19 @@ SCRAPE_WORKAROUND:
266 266
267 /* Scanned whole query string, no hash means full scrape... you might want to limit that */ 267 /* Scanned whole query string, no hash means full scrape... you might want to limit that */
268 if( !hash ) { 268 if( !hash ) {
269 if( ( reply_size = return_fullscrape_for_tracker( &reply ) ) <= 0 ) HTTPERROR_500; 269 if( !( reply_size = return_fullscrape_for_tracker( &reply ) ) ) HTTPERROR_500;
270 return sendmallocdata( s, reply, reply_size ); 270 return sendmallocdata( s, reply, reply_size );
271 } 271 }
272 272
273 /* Enough for http header + whole scrape string */ 273 /* Enough for http header + whole scrape string */
274 if( ( reply_size = return_scrape_for_torrent( hash, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) <= 0 ) HTTPERROR_500; 274 if( !( reply_size = return_scrape_for_torrent( hash, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) ) HTTPERROR_500;
275 break; 275 break;
276 case 8: 276 case 8:
277 if( byte_diff(data,8,"announce")) HTTPERROR_404; 277 if( byte_diff( data, 8, "announce" ) ) HTTPERROR_404;
278 278
279ANNOUNCE_WORKAROUND: 279ANNOUNCE_WORKAROUND:
280 280
281 OT_SETIP( &peer, ((struct http_data*)io_getcookie( s ))->ip); 281 OT_SETIP( &peer, ((struct http_data*)io_getcookie( s ) )->ip );
282 OT_SETPORT( &peer, &port ); 282 OT_SETPORT( &peer, &port );
283 OT_FLAG( &peer ) = 0; 283 OT_FLAG( &peer ) = 0;
284 numwant = 50; 284 numwant = 50;
@@ -292,8 +292,8 @@ ANNOUNCE_WORKAROUND:
292#ifdef WANT_IP_FROM_QUERY_STRING 292#ifdef WANT_IP_FROM_QUERY_STRING
293 case 2: 293 case 2:
294 if(!byte_diff(data,2,"ip")) { 294 if(!byte_diff(data,2,"ip")) {
295 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
296 unsigned char ip[4]; 295 unsigned char ip[4];
296 len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
297 if( ( len <= 0 ) || scan_fixed_ip( data, len, ip ) ) HTTPERROR_400_PARAM; 297 if( ( len <= 0 ) || scan_fixed_ip( data, len, ip ) ) HTTPERROR_400_PARAM;
298 OT_SETIP( &peer, ip ); 298 OT_SETIP( &peer, ip );
299 } else 299 } else
@@ -301,40 +301,39 @@ ANNOUNCE_WORKAROUND:
301 break; 301 break;
302#endif 302#endif
303 case 4: 303 case 4:
304 if(!byte_diff(data,4,"port")) { 304 if( !byte_diff( data, 4, "port" ) ) {
305 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 305 len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
306 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || ( tmp > 0xffff ) ) HTTPERROR_400_PARAM; 306 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || ( tmp > 0xffff ) ) HTTPERROR_400_PARAM;
307 port = htons( tmp ); OT_SETPORT( &peer, &port ); 307 port = htons( tmp ); OT_SETPORT( &peer, &port );
308 } else if(!byte_diff(data,4,"left")) { 308 } else if( !byte_diff( data, 4, "left" ) ) {
309 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 309 if( ( len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM;
310 if( len <= 0 ) HTTPERROR_400_PARAM;
311 if( scan_fixed_int( data, len, &tmp ) ) tmp = 0; 310 if( scan_fixed_int( data, len, &tmp ) ) tmp = 0;
312 if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING; 311 if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING;
313 } else 312 } else
314 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); 313 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
315 break; 314 break;
316 case 5: 315 case 5:
317 if(byte_diff(data,5,"event")) 316 if( byte_diff( data, 5, "event" ) )
318 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); 317 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
319 else switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) { 318 else switch( scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) {
320 case -1: 319 case -1:
321 HTTPERROR_400_PARAM; 320 HTTPERROR_400_PARAM;
322 case 7: 321 case 7:
323 if(!byte_diff(data,7,"stopped")) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED; 322 if( !byte_diff( data, 7, "stopped" ) ) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED;
324 break; 323 break;
325 case 9: 324 case 9:
326 if(!byte_diff(data,9,"completed")) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED; 325 if( !byte_diff( data, 9, "completed" ) ) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED;
327 default: /* Fall through intended */ 326 default: /* Fall through intended */
328 break; 327 break;
329 } 328 }
330 break; 329 break;
331 case 7: 330 case 7:
332 if(!byte_diff(data,7,"numwant")) { 331 if(!byte_diff(data,7,"numwant")) {
333 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 332 len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
334 if( ( len <= 0 ) || scan_fixed_int( data, len, &numwant ) ) HTTPERROR_400_PARAM; 333 if( ( len <= 0 ) || scan_fixed_int( data, len, &numwant ) ) HTTPERROR_400_PARAM;
335 if( numwant > 200 ) numwant = 200; 334 if( numwant > 200 ) numwant = 200;
336 } else if(!byte_diff(data,7,"compact")) { 335 } else if(!byte_diff(data,7,"compact")) {
337 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 336 len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
338 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) HTTPERROR_400_PARAM; 337 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) HTTPERROR_400_PARAM;
339 if( !tmp ) HTTPERROR_400_COMPACT; 338 if( !tmp ) HTTPERROR_400_COMPACT;
340 } else 339 } else
@@ -352,7 +351,7 @@ ANNOUNCE_WORKAROUND:
352 } 351 }
353 } 352 }
354 353
355 /* Scanned whole query string */ 354 /* Scanned whole query string XXX better send Error */
356 if( !hash ) HTTPERROR_400_PARAM; 355 if( !hash ) HTTPERROR_400_PARAM;
357 356
358 if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) { 357 if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) {
@@ -360,15 +359,15 @@ ANNOUNCE_WORKAROUND:
360 reply_size = sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "d8:completei0e10:incompletei0e8:intervali%ie5:peers0:e", OT_CLIENT_REQUEST_INTERVAL_RANDOM ); 359 reply_size = sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "d8:completei0e10:incompletei0e8:intervali%ie5:peers0:e", OT_CLIENT_REQUEST_INTERVAL_RANDOM );
361 } else { 360 } else {
362 torrent = add_peer_to_torrent( hash, &peer ); 361 torrent = add_peer_to_torrent( hash, &peer );
363 if( !torrent || ( reply_size = return_peers_for_torrent( torrent, numwant, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) <= 0 ) HTTPERROR_500; 362 if( !torrent || !( reply_size = return_peers_for_torrent( torrent, numwant, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf ) ) ) HTTPERROR_500;
364 } 363 }
365 ot_overall_successfulannounces++; 364 ot_overall_successfulannounces++;
366 break; 365 break;
367 case 10: 366 case 10:
368 if( byte_diff(data,10,"scrape.php")) HTTPERROR_404; 367 if( byte_diff( data, 10, "scrape.php" ) ) HTTPERROR_404;
369 goto SCRAPE_WORKAROUND; 368 goto SCRAPE_WORKAROUND;
370 case 11: 369 case 11:
371 if( byte_diff(data,11,"mrtg_scrape")) HTTPERROR_404; 370 if( byte_diff( data, 11, "mrtg_scrape" ) ) HTTPERROR_404;
372 371
373 t = time( NULL ) - ot_start_time; 372 t = time( NULL ) - ot_start_time;
374 reply_size = sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, 373 reply_size = sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH,
@@ -376,13 +375,13 @@ ANNOUNCE_WORKAROUND:
376 ot_overall_connections, ot_overall_successfulannounces, (int)t, (int)(t / 3600), (int)ot_overall_connections / ( (int)t ? (int)t : 1 ) ); 375 ot_overall_connections, ot_overall_successfulannounces, (int)t, (int)(t / 3600), (int)ot_overall_connections / ( (int)t ? (int)t : 1 ) );
377 break; 376 break;
378 case 12: 377 case 12:
379 if( byte_diff(data,12,"announce.php")) HTTPERROR_404; 378 if( byte_diff( data, 12, "announce.php" ) ) HTTPERROR_404;
380 goto ANNOUNCE_WORKAROUND; 379 goto ANNOUNCE_WORKAROUND;
381 default: /* neither *scrape nor announce */ 380 default: /* neither *scrape nor announce */
382 HTTPERROR_404; 381 HTTPERROR_404;
383 } 382 }
384 383
385 if( reply_size <= 0 ) HTTPERROR_500; 384 if( !reply_size ) HTTPERROR_500;
386 385
387 /* This one is rather ugly, so I take you step by step through it. 386 /* This one is rather ugly, so I take you step by step through it.
388 387
@@ -449,7 +448,7 @@ static void help( char *name ) {
449 448
450static void handle_read( const int64 clientsocket ) { 449static void handle_read( const int64 clientsocket ) {
451 struct http_data* h = io_getcookie( clientsocket ); 450 struct http_data* h = io_getcookie( clientsocket );
452 int64 l; 451 ssize_t l;
453 452
454 if( ( l = io_tryread( clientsocket, static_inbuf, sizeof static_inbuf ) ) <= 0 ) { 453 if( ( l = io_tryread( clientsocket, static_inbuf, sizeof static_inbuf ) ) <= 0 ) {
455 if( h ) { 454 if( h ) {