summaryrefslogtreecommitdiff
path: root/ot_http.c
diff options
context:
space:
mode:
authorerdgeist <>2010-04-22 22:08:42 +0000
committererdgeist <>2010-04-22 22:08:42 +0000
commitd42bf5a0310b8df4babff645ee91c37e9f994bfe (patch)
tree878712aeae9d2c64dc626b2945f11cac50954258 /ot_http.c
parentae9ab769415f30ccb444bb0b0190a5fcf22275e7 (diff)
** struct ot_workstruct gets ritcher (and will become even ritcher soon).
This is where we encapsulate all per-request data from peer to hash to peer_id, so that it is available everywhere without passing hundreds of pointers down the stack. Most functions that do work down the stack now accept an ot_workstruct and some flags. So it can end up in the stats/event-handler where it will be the default parameter in the future. ** peer_id is now being copied by default and moved to ot_workstruct So it is available in stats and subsequent functions. ** sync scrape madness is gone SYNC_SCRAPE was intended to sync tracker state that would normally be lost on restarts i.e. downloaded counts per torrent. The way was to push it in the tracker cloud after finding all neighbouring trackers. This is madness. It never was tested and can be done per tracker by fetching stats/mode=statedump from time to time and starting opentracker with the -l option later. ** livesync thread has its own ot_workstruct now So it can behave like ot_udp and ot_http against trackerlogic.c and get rid of the first half of the embarrassing global variables. The sending half will be fixed soon [tm]. ** stats can log completed events The author recognizes the needs of original content distributors to keep track of the amount of times a work has been downloaded. While not feasible and used on openbittorrent and other open and anonymous tracker installations, a tracker user can now choose to send those events to syslog.
Diffstat (limited to 'ot_http.c')
-rw-r--r--ot_http.c45
1 files changed, 27 insertions, 18 deletions
diff --git a/ot_http.c b/ot_http.c
index c544468..567cba3 100644
--- a/ot_http.c
+++ b/ot_http.c
@@ -369,12 +369,11 @@ static ot_keywords keywords_announce[] = { { "port", 1 }, { "left", 2 }, { "even
369#ifdef WANT_FULLLOG_NETWORKS 369#ifdef WANT_FULLLOG_NETWORKS
370{ "lognet", 8 }, 370{ "lognet", 8 },
371#endif 371#endif
372{ "peer_id", 9 },
372{ NULL, -3 } }; 373{ NULL, -3 } };
373static ot_keywords keywords_announce_event[] = { { "completed", 1 }, { "stopped", 2 }, { NULL, -3 } }; 374static ot_keywords keywords_announce_event[] = { { "completed", 1 }, { "stopped", 2 }, { NULL, -3 } };
374static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws, char *read_ptr ) { 375static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws, char *read_ptr ) {
375 int numwant, tmp, scanon; 376 int numwant, tmp, scanon;
376 ot_peer peer;
377 ot_hash *hash = NULL;
378 unsigned short port = 0; 377 unsigned short port = 0;
379 char *write_ptr; 378 char *write_ptr;
380 ssize_t len; 379 ssize_t len;
@@ -392,14 +391,18 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
392 ot_ip6 proxied_ip; 391 ot_ip6 proxied_ip;
393 char *fwd = http_header( ws->request, ws->header_size, "x-forwarded-for" ); 392 char *fwd = http_header( ws->request, ws->header_size, "x-forwarded-for" );
394 if( fwd && scan_ip6( fwd, proxied_ip ) ) 393 if( fwd && scan_ip6( fwd, proxied_ip ) )
395 OT_SETIP( &peer, proxied_ip ); 394 OT_SETIP( &ws->peer, proxied_ip );
396 else 395 else
397 OT_SETIP( &peer, cookie->ip ); 396 OT_SETIP( &ws->peer, cookie->ip );
398 } else 397 } else
399#endif 398#endif
400 OT_SETIP( &peer, cookie->ip ); 399
401 OT_SETPORT( &peer, &port ); 400 ws->peer_id = NULL;
402 OT_PEERFLAG( &peer ) = 0; 401 ws->hash = NULL;
402
403 OT_SETIP( &ws->peer, cookie->ip );
404 OT_SETPORT( &ws->peer, &port );
405 OT_PEERFLAG( &ws->peer ) = 0;
403 numwant = 50; 406 numwant = 50;
404 scanon = 1; 407 scanon = 1;
405 408
@@ -411,21 +414,21 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
411 case 1: /* matched "port" */ 414 case 1: /* matched "port" */
412 len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ); 415 len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE );
413 if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &tmp ) || ( tmp > 0xffff ) ) HTTPERROR_400_PARAM; 416 if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &tmp ) || ( tmp > 0xffff ) ) HTTPERROR_400_PARAM;
414 port = htons( tmp ); OT_SETPORT( &peer, &port ); 417 port = htons( tmp ); OT_SETPORT( &ws->peer, &port );
415 break; 418 break;
416 case 2: /* matched "left" */ 419 case 2: /* matched "left" */
417 if( ( len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM; 420 if( ( len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM;
418 if( scan_fixed_int( write_ptr, len, &tmp ) ) tmp = 0; 421 if( scan_fixed_int( write_ptr, len, &tmp ) ) tmp = 0;
419 if( !tmp ) OT_PEERFLAG( &peer ) |= PEER_FLAG_SEEDING; 422 if( !tmp ) OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING;
420 break; 423 break;
421 case 3: /* matched "event" */ 424 case 3: /* matched "event" */
422 switch( scan_find_keywords( keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) { 425 switch( scan_find_keywords( keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) {
423 case -1: HTTPERROR_400_PARAM; 426 case -1: HTTPERROR_400_PARAM;
424 case 1: /* matched "completed" */ 427 case 1: /* matched "completed" */
425 OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; 428 OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED;
426 break; 429 break;
427 case 2: /* matched "stopped" */ 430 case 2: /* matched "stopped" */
428 OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; 431 OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED;
429 break; 432 break;
430 default: 433 default:
431 break; 434 break;
@@ -443,10 +446,10 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
443 if( !tmp ) HTTPERROR_400_COMPACT; 446 if( !tmp ) HTTPERROR_400_COMPACT;
444 break; 447 break;
445 case 6: /* matched "info_hash" */ 448 case 6: /* matched "info_hash" */
446 if( hash ) HTTPERROR_400_DOUBLEHASH; 449 if( ws->hash ) HTTPERROR_400_DOUBLEHASH;
447 /* ignore this, when we have less than 20 bytes */ 450 /* ignore this, when we have less than 20 bytes */
448 if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM; 451 if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM;
449 hash = (ot_hash*)write_ptr; 452 ws->hash = (ot_hash*)write_ptr;
450 break; 453 break;
451#ifdef WANT_IP_FROM_QUERY_STRING 454#ifdef WANT_IP_FROM_QUERY_STRING
452 case 7: /* matched "ip" */ 455 case 7: /* matched "ip" */
@@ -455,7 +458,7 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
455 len = scan_urlencoded_query( &read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE ); 458 len = scan_urlencoded_query( &read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE );
456 tmp_buf2[len] = 0; 459 tmp_buf2[len] = 0;
457 if( ( len <= 0 ) || !scan_ip6( tmp_buf2, tmp_buf1 ) ) HTTPERROR_400_PARAM; 460 if( ( len <= 0 ) || !scan_ip6( tmp_buf2, tmp_buf1 ) ) HTTPERROR_400_PARAM;
458 OT_SETIP( &peer, tmp_buf1 ); 461 OT_SETIP( &ws->peer, tmp_buf1 );
459 } 462 }
460 break; 463 break;
461#endif 464#endif
@@ -490,6 +493,12 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
490 //} 493 //}
491 } 494 }
492#endif 495#endif
496 break;
497 case 9: /* matched "peer_id" */
498 /* ignore this, when we have less than 20 bytes */
499 if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM;
500 ws->peer_id = write_ptr;
501 break;
493 } 502 }
494 } 503 }
495 504
@@ -501,13 +510,13 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
501 stats_issue_event( EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ws->reply ); 510 stats_issue_event( EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ws->reply );
502 511
503 /* Scanned whole query string */ 512 /* Scanned whole query string */
504 if( !hash ) 513 if( !ws->hash )
505 return ws->reply_size = sprintf( ws->reply, "d14:failure reason80:Your client forgot to send your torrent's info_hash. Please upgrade your client.e" ); 514 return ws->reply_size = sprintf( ws->reply, "d14:failure reason80:Your client forgot to send your torrent's info_hash. Please upgrade your client.e" );
506 515
507 if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) 516 if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED )
508 ws->reply_size = remove_peer_from_torrent( *hash, &peer, ws->reply, FLAG_TCP ); 517 ws->reply_size = remove_peer_from_torrent( FLAG_TCP, ws );
509 else 518 else
510 ws->reply_size = add_peer_to_torrent_and_return_peers( *hash, &peer, FLAG_TCP, numwant, ws->reply ); 519 ws->reply_size = add_peer_to_torrent_and_return_peers( FLAG_TCP, ws, numwant );
511 520
512 stats_issue_event( EVENT_ANNOUNCE, FLAG_TCP, ws->reply_size); 521 stats_issue_event( EVENT_ANNOUNCE, FLAG_TCP, ws->reply_size);
513 return ws->reply_size; 522 return ws->reply_size;