|
|
|
@ -4,31 +4,31 @@
@@ -4,31 +4,31 @@
|
|
|
|
|
$id$ */ |
|
|
|
|
|
|
|
|
|
/* System */ |
|
|
|
|
#include <sys/types.h> |
|
|
|
|
#include <arpa/inet.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <pthread.h> |
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <sys/types.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include <pthread.h> |
|
|
|
|
|
|
|
|
|
/* Libowfat */ |
|
|
|
|
#include "byte.h" |
|
|
|
|
#include "array.h" |
|
|
|
|
#include "byte.h" |
|
|
|
|
#include "case.h" |
|
|
|
|
#include "iob.h" |
|
|
|
|
#include "ip6.h" |
|
|
|
|
#include "scan.h" |
|
|
|
|
#include "case.h" |
|
|
|
|
|
|
|
|
|
/* Opentracker */ |
|
|
|
|
#include "trackerlogic.h" |
|
|
|
|
#include "ot_mutex.h" |
|
|
|
|
#include "ot_accesslist.h" |
|
|
|
|
#include "ot_fullscrape.h" |
|
|
|
|
#include "ot_http.h" |
|
|
|
|
#include "ot_iovec.h" |
|
|
|
|
#include "scan_urlencoded_query.h" |
|
|
|
|
#include "ot_fullscrape.h" |
|
|
|
|
#include "ot_mutex.h" |
|
|
|
|
#include "ot_stats.h" |
|
|
|
|
#include "ot_accesslist.h" |
|
|
|
|
#include "scan_urlencoded_query.h" |
|
|
|
|
#include "trackerlogic.h" |
|
|
|
|
|
|
|
|
|
#define OT_MAXMULTISCRAPE_COUNT 64 |
|
|
|
|
#define OT_BATCH_LIMIT (1024 * 1024 * 16) |
|
|
|
@ -37,15 +37,16 @@ extern char *g_redirecturl;
@@ -37,15 +37,16 @@ extern char *g_redirecturl;
|
|
|
|
|
char *g_stats_path; |
|
|
|
|
ssize_t g_stats_path_len; |
|
|
|
|
|
|
|
|
|
enum { |
|
|
|
|
SUCCESS_HTTP_HEADER_LENGTH = 80, |
|
|
|
|
SUCCESS_HTTP_SIZE_OFF = 17 }; |
|
|
|
|
enum { SUCCESS_HTTP_HEADER_LENGTH = 80, SUCCESS_HTTP_SIZE_OFF = 17 }; |
|
|
|
|
|
|
|
|
|
static void http_senddata(const int64 sock, struct ot_workstruct *ws) { |
|
|
|
|
struct http_data *cookie = io_getcookie(sock); |
|
|
|
|
ssize_t written_size; |
|
|
|
|
|
|
|
|
|
if( !cookie ) { io_close(sock); return; } |
|
|
|
|
if (!cookie) { |
|
|
|
|
io_close(sock); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* whoever sends data is not interested in its input-array */ |
|
|
|
|
if (ws->keep_alive && ws->header_size != ws->request_size) { |
|
|
|
@ -61,7 +62,9 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
@@ -61,7 +62,9 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
|
|
|
|
|
written_size = write(sock, ws->reply, ws->reply_size); |
|
|
|
|
if ((written_size < 0) || ((written_size == ws->reply_size) && !ws->keep_alive)) { |
|
|
|
|
array_reset(&cookie->request); |
|
|
|
|
free( cookie ); io_close( sock ); return; |
|
|
|
|
free(cookie); |
|
|
|
|
io_close(sock); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (written_size < ws->reply_size) { |
|
|
|
@ -70,7 +73,8 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
@@ -70,7 +73,8 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
|
|
|
|
|
|
|
|
|
|
if (!(outbuf = malloc(ws->reply_size - written_size))) { |
|
|
|
|
array_reset(&cookie->request); |
|
|
|
|
free(cookie); io_close( sock ); |
|
|
|
|
free(cookie); |
|
|
|
|
io_close(sock); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -85,7 +89,8 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
@@ -85,7 +89,8 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
|
|
|
|
|
|
|
|
|
|
/* writeable short data sockets just have a tcp timeout */ |
|
|
|
|
if (!ws->keep_alive) { |
|
|
|
|
taia_uint( &t, 0 ); io_timeout( sock, t ); |
|
|
|
|
taia_uint(&t, 0); |
|
|
|
|
io_timeout(sock, t); |
|
|
|
|
io_dontwantread(sock); |
|
|
|
|
} |
|
|
|
|
io_wantwrite(sock); |
|
|
|
@ -110,7 +115,8 @@ ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code )
@@ -110,7 +115,8 @@ ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code )
|
|
|
|
|
if (code == CODE_HTTPERROR_302) |
|
|
|
|
ws->reply_size = snprintf(ws->reply, G_OUTBUF_SIZE, "HTTP/1.0 302 Found\r\nContent-Length: 0\r\nLocation: %s\r\n\r\n", g_redirecturl); |
|
|
|
|
else |
|
|
|
|
ws->reply_size = snprintf( ws->reply, G_OUTBUF_SIZE, "HTTP/1.0 %s\r\nContent-Type: text/html\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n", title, strlen(title)+16-4,title+4); |
|
|
|
|
ws->reply_size = snprintf(ws->reply, G_OUTBUF_SIZE, "HTTP/1.0 %s\r\nContent-Type: text/html\r\nContent-Length: %zd\r\n\r\n<title>%s</title>\n", title, |
|
|
|
|
strlen(title) + 16 - 4, title + 4); |
|
|
|
|
|
|
|
|
|
#ifdef _DEBUG_HTTPERROR |
|
|
|
|
fprintf(stderr, "DEBUG: invalid request was: %s\n", ws->debugbuf); |
|
|
|
@ -155,7 +161,8 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
@@ -155,7 +161,8 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
|
|
|
|
|
header_size = asprintf(&header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n%sContent-Length: %zd\r\n\r\n", encoding, size); |
|
|
|
|
else { |
|
|
|
|
if (!(cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)) { |
|
|
|
|
header_size = asprintf( &header, "HTTP/1.0 200 OK\r\nContent-Type: application/octet-stream\r\n%sTransfer-Encoding: chunked\r\n\r\n%zx\r\n", encoding, size ); |
|
|
|
|
header_size = |
|
|
|
|
asprintf(&header, "HTTP/1.0 200 OK\r\nContent-Type: application/octet-stream\r\n%sTransfer-Encoding: chunked\r\n\r\n%zx\r\n", encoding, size); |
|
|
|
|
cookie->flag |= STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER; |
|
|
|
|
} else |
|
|
|
|
header_size = asprintf(&header, "%zx\r\n", size); |
|
|
|
@ -203,29 +210,43 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
@@ -203,29 +210,43 @@ ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iove
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* writeable sockets timeout after 10 minutes */ |
|
|
|
|
taia_now( &t ); taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_SEND ); |
|
|
|
|
taia_now(&t); |
|
|
|
|
taia_addsec(&t, &t, OT_CLIENT_TIMEOUT_SEND); |
|
|
|
|
io_timeout(sock, t); |
|
|
|
|
io_wantwrite(sock); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static ssize_t http_handle_stats(const int64 sock, struct ot_workstruct *ws, char *read_ptr) { |
|
|
|
|
static const ot_keywords keywords_main[] = |
|
|
|
|
{ { "mode", 1 }, {"format", 2 }, {"info_hash", 3}, { NULL, -3 } }; |
|
|
|
|
static const ot_keywords keywords_mode[] = |
|
|
|
|
{ { "peer", TASK_STATS_PEERS }, { "conn", TASK_STATS_CONNS }, { "scrp", TASK_STATS_SCRAPE }, { "udp4", TASK_STATS_UDP }, { "tcp4", TASK_STATS_TCP }, |
|
|
|
|
{ "busy", TASK_STATS_BUSY_NETWORKS }, { "torr", TASK_STATS_TORRENTS }, { "fscr", TASK_STATS_FULLSCRAPE }, |
|
|
|
|
{ "s24s", TASK_STATS_SLASH24S }, { "tpbs", TASK_STATS_TPB }, { "herr", TASK_STATS_HTTPERRORS }, { "completed", TASK_STATS_COMPLETED }, |
|
|
|
|
{ "top100", TASK_STATS_TOP100 }, { "top10", TASK_STATS_TOP10 }, { "renew", TASK_STATS_RENEW }, { "syncs", TASK_STATS_SYNCS }, { "version", TASK_STATS_VERSION }, |
|
|
|
|
{ "everything", TASK_STATS_EVERYTHING }, { "statedump", TASK_FULLSCRAPE_TRACKERSTATE }, { "fulllog", TASK_STATS_FULLLOG }, |
|
|
|
|
static const ot_keywords keywords_main[] = {{"mode", 1}, {"format", 2}, {"info_hash", 3}, {NULL, -3}}; |
|
|
|
|
static const ot_keywords keywords_mode[] = {{"peer", TASK_STATS_PEERS}, |
|
|
|
|
{"conn", TASK_STATS_CONNS}, |
|
|
|
|
{"scrp", TASK_STATS_SCRAPE}, |
|
|
|
|
{"udp4", TASK_STATS_UDP}, |
|
|
|
|
{"tcp4", TASK_STATS_TCP}, |
|
|
|
|
{"busy", TASK_STATS_BUSY_NETWORKS}, |
|
|
|
|
{"torr", TASK_STATS_TORRENTS}, |
|
|
|
|
{"fscr", TASK_STATS_FULLSCRAPE}, |
|
|
|
|
{"s24s", TASK_STATS_SLASH24S}, |
|
|
|
|
{"tpbs", TASK_STATS_TPB}, |
|
|
|
|
{"herr", TASK_STATS_HTTPERRORS}, |
|
|
|
|
{"completed", TASK_STATS_COMPLETED}, |
|
|
|
|
{"top100", TASK_STATS_TOP100}, |
|
|
|
|
{"top10", TASK_STATS_TOP10}, |
|
|
|
|
{"renew", TASK_STATS_RENEW}, |
|
|
|
|
{"syncs", TASK_STATS_SYNCS}, |
|
|
|
|
{"version", TASK_STATS_VERSION}, |
|
|
|
|
{"everything", TASK_STATS_EVERYTHING}, |
|
|
|
|
{"statedump", TASK_FULLSCRAPE_TRACKERSTATE}, |
|
|
|
|
{"fulllog", TASK_STATS_FULLLOG}, |
|
|
|
|
{"woodpeckers", TASK_STATS_WOODPECKERS}, |
|
|
|
|
#ifdef WANT_LOG_NUMWANT |
|
|
|
|
{"numwants", TASK_STATS_NUMWANTS}, |
|
|
|
|
#endif |
|
|
|
|
{NULL, -3}}; |
|
|
|
|
static const ot_keywords keywords_format[] = |
|
|
|
|
{ { "bin", TASK_FULLSCRAPE_TPB_BINARY }, { "ben", TASK_FULLSCRAPE }, { "url", TASK_FULLSCRAPE_TPB_URLENCODED }, |
|
|
|
|
{ "txt", TASK_FULLSCRAPE_TPB_ASCII }, { "txtp", TASK_FULLSCRAPE_TPB_ASCII_PLUS }, { NULL, -3 } }; |
|
|
|
|
static const ot_keywords keywords_format[] = {{"bin", TASK_FULLSCRAPE_TPB_BINARY}, {"ben", TASK_FULLSCRAPE}, |
|
|
|
|
{"url", TASK_FULLSCRAPE_TPB_URLENCODED}, {"txt", TASK_FULLSCRAPE_TPB_ASCII}, |
|
|
|
|
{"txtp", TASK_FULLSCRAPE_TPB_ASCII_PLUS}, {NULL, -3}}; |
|
|
|
|
|
|
|
|
|
int mode = TASK_STATS_PEERS, scanon = 1, format = 0; |
|
|
|
|
|
|
|
|
@ -238,22 +259,31 @@ static const ot_keywords keywords_format[] =
@@ -238,22 +259,31 @@ static const ot_keywords keywords_format[] =
|
|
|
|
|
|
|
|
|
|
while (scanon) { |
|
|
|
|
switch (scan_find_keywords(keywords_main, &read_ptr, SCAN_SEARCHPATH_PARAM)) { |
|
|
|
|
case -2: scanon = 0; break; /* TERMINATOR */ |
|
|
|
|
case -1: HTTPERROR_400_PARAM; /* PARSE ERROR */ |
|
|
|
|
case -3: scan_urlencoded_skipvalue( &read_ptr ); break; |
|
|
|
|
case -2: |
|
|
|
|
scanon = 0; |
|
|
|
|
break; /* TERMINATOR */ |
|
|
|
|
case -1: |
|
|
|
|
HTTPERROR_400_PARAM; /* PARSE ERROR */ |
|
|
|
|
case -3: |
|
|
|
|
scan_urlencoded_skipvalue(&read_ptr); |
|
|
|
|
break; |
|
|
|
|
case 1: /* matched "mode" */ |
|
|
|
|
if( ( mode = scan_find_keywords( keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM; |
|
|
|
|
if ((mode = scan_find_keywords(keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
break; |
|
|
|
|
case 2: /* matched "format" */ |
|
|
|
|
if( ( format = scan_find_keywords( keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM; |
|
|
|
|
if ((format = scan_find_keywords(keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
break; |
|
|
|
|
case 3: HTTPERROR_400_PARAM; /* If the stats URL was mistakenly added as announce URL, return a 400 */ |
|
|
|
|
case 3: |
|
|
|
|
HTTPERROR_400_PARAM; /* If the stats URL was mistakenly added as announce URL, return a 400 */ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#ifdef WANT_FULLSCRAPE |
|
|
|
|
if (mode == TASK_FULLSCRAPE_TRACKERSTATE) { |
|
|
|
|
format = mode; mode = TASK_STATS_TPB; |
|
|
|
|
format = mode; |
|
|
|
|
mode = TASK_STATS_TPB; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (mode == TASK_STATS_TPB) { |
|
|
|
@ -274,7 +304,8 @@ static const ot_keywords keywords_format[] =
@@ -274,7 +304,8 @@ static const ot_keywords keywords_format[] =
|
|
|
|
|
cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; |
|
|
|
|
|
|
|
|
|
/* Clients waiting for us should not easily timeout */ |
|
|
|
|
taia_uint( &t, 0 ); io_timeout( sock, t ); |
|
|
|
|
taia_uint(&t, 0); |
|
|
|
|
io_timeout(sock, t); |
|
|
|
|
fullscrape_deliver(sock, format); |
|
|
|
|
io_dontwantread(sock); |
|
|
|
|
return ws->reply_size = -2; |
|
|
|
@ -285,7 +316,8 @@ static const ot_keywords keywords_format[] =
@@ -285,7 +316,8 @@ static const ot_keywords keywords_format[] =
|
|
|
|
|
if ((mode & TASK_CLASS_MASK) == TASK_STATS) { |
|
|
|
|
tai6464 t; |
|
|
|
|
/* Complex stats also include expensive memory debugging tools */ |
|
|
|
|
taia_uint( &t, 0 ); io_timeout( sock, t ); |
|
|
|
|
taia_uint(&t, 0); |
|
|
|
|
io_timeout(sock, t); |
|
|
|
|
stats_deliver(sock, mode); |
|
|
|
|
return ws->reply_size = -2; |
|
|
|
|
} |
|
|
|
@ -297,7 +329,10 @@ static const ot_keywords keywords_format[] =
@@ -297,7 +329,10 @@ static const ot_keywords keywords_format[] =
|
|
|
|
|
#ifdef WANT_MODEST_FULLSCRAPES |
|
|
|
|
static pthread_mutex_t g_modest_fullscrape_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
|
|
|
static ot_vector g_modest_fullscrape_timeouts; |
|
|
|
|
typedef struct { ot_ip6 ip; ot_time last_fullscrape; } ot_scrape_log; |
|
|
|
|
typedef struct { |
|
|
|
|
ot_ip6 ip; |
|
|
|
|
ot_time last_fullscrape; |
|
|
|
|
} ot_scrape_log; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
#ifdef WANT_FULLSCRAPE |
|
|
|
@ -348,7 +383,8 @@ static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *w
@@ -348,7 +383,8 @@ static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *w
|
|
|
|
|
/* Pass this task to the worker thread */ |
|
|
|
|
cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; |
|
|
|
|
/* Clients waiting for us should not easily timeout */ |
|
|
|
|
taia_uint( &t, 0 ); io_timeout( sock, t ); |
|
|
|
|
taia_uint(&t, 0); |
|
|
|
|
io_timeout(sock, t); |
|
|
|
|
fullscrape_deliver(sock, TASK_FULLSCRAPE | format); |
|
|
|
|
io_dontwantread(sock); |
|
|
|
|
return ws->reply_size = -2; |
|
|
|
@ -363,16 +399,23 @@ static ssize_t http_handle_scrape( const int64 sock, struct ot_workstruct *ws, c
@@ -363,16 +399,23 @@ static ssize_t http_handle_scrape( const int64 sock, struct ot_workstruct *ws, c
|
|
|
|
|
|
|
|
|
|
/* This is to hack around stupid clients that send "scrape ?info_hash" */ |
|
|
|
|
if (read_ptr[-1] != '?') { |
|
|
|
|
while( ( *read_ptr != '?' ) && ( *read_ptr != '\n' ) ) ++read_ptr; |
|
|
|
|
if( *read_ptr == '\n' ) HTTPERROR_400_PARAM; |
|
|
|
|
while ((*read_ptr != '?') && (*read_ptr != '\n')) |
|
|
|
|
++read_ptr; |
|
|
|
|
if (*read_ptr == '\n') |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
++read_ptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (scanon) { |
|
|
|
|
switch (scan_find_keywords(keywords_scrape, &read_ptr, SCAN_SEARCHPATH_PARAM)) { |
|
|
|
|
case -2: scanon = 0; break; /* TERMINATOR */ |
|
|
|
|
default: HTTPERROR_400_PARAM; /* PARSE ERROR */ |
|
|
|
|
case -3: scan_urlencoded_skipvalue( &read_ptr ); break; |
|
|
|
|
case -2: |
|
|
|
|
scanon = 0; |
|
|
|
|
break; /* TERMINATOR */ |
|
|
|
|
default: |
|
|
|
|
HTTPERROR_400_PARAM; /* PARSE ERROR */ |
|
|
|
|
case -3: |
|
|
|
|
scan_urlencoded_skipvalue(&read_ptr); |
|
|
|
|
break; |
|
|
|
|
case 1: /* matched "info_hash" */ |
|
|
|
|
/* ignore this, when we have less than 20 bytes */ |
|
|
|
|
if (scan_urlencoded_query(&read_ptr, (char *)(multiscrape_buf + numwant++), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash)) |
|
|
|
@ -382,7 +425,8 @@ static ssize_t http_handle_scrape( const int64 sock, struct ot_workstruct *ws, c
@@ -382,7 +425,8 @@ static ssize_t http_handle_scrape( const int64 sock, struct ot_workstruct *ws, c
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* No info_hash found? Inform user */ |
|
|
|
|
if( !numwant ) HTTPERROR_400_PARAM; |
|
|
|
|
if (!numwant) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
|
|
|
|
|
/* Limit number of hashes to process */ |
|
|
|
|
if (numwant > OT_MAXMULTISCRAPE_COUNT) |
|
|
|
@ -403,10 +447,13 @@ static char* http_header( char *data, size_t byte_count, char *header ) {
@@ -403,10 +447,13 @@ static char* http_header( char *data, size_t byte_count, char *header ) {
|
|
|
|
|
size_t i; |
|
|
|
|
long sl = strlen(header); |
|
|
|
|
for (i = 0; i + sl + 2 < byte_count; ++i) { |
|
|
|
|
if( data[i] != '\n' || data[ i + sl + 1] != ':' ) continue; |
|
|
|
|
if( !case_equalb( data + i + 1, sl, header ) ) continue; |
|
|
|
|
if (data[i] != '\n' || data[i + sl + 1] != ':') |
|
|
|
|
continue; |
|
|
|
|
if (!case_equalb(data + i + 1, sl, header)) |
|
|
|
|
continue; |
|
|
|
|
data += i + sl + 2; |
|
|
|
|
while( *data == ' ' || *data == '\t' ) ++data; |
|
|
|
|
while (*data == ' ' || *data == '\t') |
|
|
|
|
++data; |
|
|
|
|
return data; |
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
@ -420,8 +467,7 @@ static ot_keywords keywords_announce[] = { { "port", 1 }, { "left", 2 }, { "even
@@ -420,8 +467,7 @@ static ot_keywords keywords_announce[] = { { "port", 1 }, { "left", 2 }, { "even
|
|
|
|
|
#ifdef WANT_FULLLOG_NETWORKS |
|
|
|
|
{"lognet", 8}, |
|
|
|
|
#endif |
|
|
|
|
{ "peer_id", 9 }, |
|
|
|
|
{ NULL, -3 } }; |
|
|
|
|
{"peer_id", 9}, {NULL, -3}}; |
|
|
|
|
static ot_keywords keywords_announce_event[] = {{"completed", 1}, {"stopped", 2}, {NULL, -3}}; |
|
|
|
|
static ssize_t http_handle_announce(const int64 sock, struct ot_workstruct *ws, char *read_ptr) { |
|
|
|
|
int numwant, tmp, scanon; |
|
|
|
@ -432,8 +478,10 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
@@ -432,8 +478,10 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
|
|
|
|
|
|
|
|
|
|
/* This is to hack around stupid clients that send "announce ?info_hash" */ |
|
|
|
|
if (read_ptr[-1] != '?') { |
|
|
|
|
while( ( *read_ptr != '?' ) && ( *read_ptr != '\n' ) ) ++read_ptr; |
|
|
|
|
if( *read_ptr == '\n' ) HTTPERROR_400_PARAM; |
|
|
|
|
while ((*read_ptr != '?') && (*read_ptr != '\n')) |
|
|
|
|
++read_ptr; |
|
|
|
|
if (*read_ptr == '\n') |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
++read_ptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -459,22 +507,33 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
@@ -459,22 +507,33 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
|
|
|
|
|
|
|
|
|
|
while (scanon) { |
|
|
|
|
switch (scan_find_keywords(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM)) { |
|
|
|
|
case -2: scanon = 0; break; /* TERMINATOR */ |
|
|
|
|
case -1: HTTPERROR_400_PARAM; /* PARSE ERROR */ |
|
|
|
|
case -3: scan_urlencoded_skipvalue( &read_ptr ); break; |
|
|
|
|
case -2: |
|
|
|
|
scanon = 0; |
|
|
|
|
break; /* TERMINATOR */ |
|
|
|
|
case -1: |
|
|
|
|
HTTPERROR_400_PARAM; /* PARSE ERROR */ |
|
|
|
|
case -3: |
|
|
|
|
scan_urlencoded_skipvalue(&read_ptr); |
|
|
|
|
break; |
|
|
|
|
case 1: /* matched "port" */ |
|
|
|
|
len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); |
|
|
|
|
if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &tmp ) || ( tmp > 0xffff ) ) HTTPERROR_400_PARAM; |
|
|
|
|
port = htons( tmp ); OT_SETPORT( &ws->peer, &port ); |
|
|
|
|
if ((len <= 0) || scan_fixed_int(write_ptr, len, &tmp) || (tmp > 0xffff)) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
port = htons(tmp); |
|
|
|
|
OT_SETPORT(&ws->peer, &port); |
|
|
|
|
break; |
|
|
|
|
case 2: /* matched "left" */ |
|
|
|
|
if( ( len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM; |
|
|
|
|
if( scan_fixed_int( write_ptr, len, &tmp ) ) tmp = 0; |
|
|
|
|
if( !tmp ) OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING; |
|
|
|
|
if ((len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
if (scan_fixed_int(write_ptr, len, &tmp)) |
|
|
|
|
tmp = 0; |
|
|
|
|
if (!tmp) |
|
|
|
|
OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING; |
|
|
|
|
break; |
|
|
|
|
case 3: /* matched "event" */ |
|
|
|
|
switch (scan_find_keywords(keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE)) { |
|
|
|
|
case -1: HTTPERROR_400_PARAM; |
|
|
|
|
case -1: |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
case 1: /* matched "completed" */ |
|
|
|
|
OT_PEERFLAG(&ws->peer) |= PEER_FLAG_COMPLETED; |
|
|
|
|
break; |
|
|
|
@ -487,19 +546,26 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
@@ -487,19 +546,26 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
|
|
|
|
|
break; |
|
|
|
|
case 4: /* matched "numwant" */ |
|
|
|
|
len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); |
|
|
|
|
if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &numwant ) ) HTTPERROR_400_PARAM; |
|
|
|
|
if( numwant < 0 ) numwant = 50; |
|
|
|
|
if( numwant > 200 ) numwant = 200; |
|
|
|
|
if ((len <= 0) || scan_fixed_int(write_ptr, len, &numwant)) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
if (numwant < 0) |
|
|
|
|
numwant = 50; |
|
|
|
|
if (numwant > 200) |
|
|
|
|
numwant = 200; |
|
|
|
|
break; |
|
|
|
|
case 5: /* matched "compact" */ |
|
|
|
|
len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); |
|
|
|
|
if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &tmp ) ) HTTPERROR_400_PARAM; |
|
|
|
|
if( !tmp ) HTTPERROR_400_COMPACT; |
|
|
|
|
if ((len <= 0) || scan_fixed_int(write_ptr, len, &tmp)) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
if (!tmp) |
|
|
|
|
HTTPERROR_400_COMPACT; |
|
|
|
|
break; |
|
|
|
|
case 6: /* matched "info_hash" */ |
|
|
|
|
if( ws->hash ) HTTPERROR_400_DOUBLEHASH; |
|
|
|
|
if (ws->hash) |
|
|
|
|
HTTPERROR_400_DOUBLEHASH; |
|
|
|
|
/* ignore this, when we have less than 20 bytes */ |
|
|
|
|
if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM; |
|
|
|
|
if (scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
ws->hash = (ot_hash *)write_ptr; |
|
|
|
|
break; |
|
|
|
|
#ifdef WANT_IP_FROM_QUERY_STRING |
|
|
|
@ -508,10 +574,10 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
@@ -508,10 +574,10 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
|
|
|
|
|
char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; |
|
|
|
|
len = scan_urlencoded_query(&read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE); |
|
|
|
|
tmp_buf2[len] = 0; |
|
|
|
|
if( ( len <= 0 ) || !scan_ip6( tmp_buf2, tmp_buf1 ) ) HTTPERROR_400_PARAM; |
|
|
|
|
if ((len <= 0) || !scan_ip6(tmp_buf2, tmp_buf1)) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
OT_SETIP(&ws->peer, tmp_buf1); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} break; |
|
|
|
|
#endif |
|
|
|
|
#ifdef WANT_FULLLOG_NETWORKS |
|
|
|
|
case 8: /* matched "lognet" */ |
|
|
|
@ -523,18 +589,21 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
@@ -523,18 +589,21 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
|
|
|
|
|
|
|
|
|
|
len = scan_urlencoded_query(&read_ptr, tmp_buf, SCAN_SEARCHPATH_VALUE); |
|
|
|
|
tmp_buf[len] = 0; |
|
|
|
|
if( len <= 0 ) HTTPERROR_400_PARAM; |
|
|
|
|
if (len <= 0) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
if (*tmp_buf == '-') { |
|
|
|
|
loglist_reset(); |
|
|
|
|
return ws->reply_size = sprintf(ws->reply, "Successfully removed.\n"); |
|
|
|
|
} |
|
|
|
|
parsed = scan_ip6(tmp_buf, net.address); |
|
|
|
|
if( !parsed ) HTTPERROR_400_PARAM; |
|
|
|
|
if (!parsed) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
if (tmp_buf[parsed++] != '/') |
|
|
|
|
bits = 128; |
|
|
|
|
else { |
|
|
|
|
parsed = scan_short(tmp_buf + parsed, &bits); |
|
|
|
|
if( !parsed ) HTTPERROR_400_PARAM; |
|
|
|
|
if (!parsed) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
if (ip6_isv4mapped(net.address)) |
|
|
|
|
bits += 96; |
|
|
|
|
} |
|
|
|
@ -542,12 +611,12 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
@@ -542,12 +611,12 @@ static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws,
|
|
|
|
|
loglist_add_network(&net); |
|
|
|
|
return ws->reply_size = sprintf(ws->reply, "Successfully added.\n"); |
|
|
|
|
//}
|
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} break; |
|
|
|
|
#endif |
|
|
|
|
case 9: /* matched "peer_id" */ |
|
|
|
|
/* ignore this, when we have less than 20 bytes */ |
|
|
|
|
if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM; |
|
|
|
|
if (scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) |
|
|
|
|
HTTPERROR_400_PARAM; |
|
|
|
|
ws->peer_id = write_ptr; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
@ -614,10 +683,12 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
@@ -614,10 +683,12 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
|
|
|
|
|
ws->reply = ws->outbuf + SUCCESS_HTTP_HEADER_LENGTH; |
|
|
|
|
|
|
|
|
|
/* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */ |
|
|
|
|
if( memcmp( read_ptr, "GET /", 5) ) HTTPERROR_400; |
|
|
|
|
if (memcmp(read_ptr, "GET /", 5)) |
|
|
|
|
HTTPERROR_400; |
|
|
|
|
|
|
|
|
|
/* Skip leading '/' */ |
|
|
|
|
for( read_ptr+=4; *read_ptr == '/'; ++read_ptr); |
|
|
|
|
for (read_ptr += 4; *read_ptr == '/'; ++read_ptr) |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
/* Try to parse the request.
|
|
|
|
|
In reality we abandoned requiring the url to be correct. This now |
|
|
|
@ -626,8 +697,10 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
@@ -626,8 +697,10 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
|
|
|
|
|
len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_PATH); |
|
|
|
|
|
|
|
|
|
/* If parsing returned an error, leave with not found */ |
|
|
|
|
if( g_redirecturl && ( len == -2 ) ) HTTPERROR_302; |
|
|
|
|
if( len <= 0 ) HTTPERROR_404; |
|
|
|
|
if (g_redirecturl && (len == -2)) |
|
|
|
|
HTTPERROR_302; |
|
|
|
|
if (len <= 0) |
|
|
|
|
HTTPERROR_404; |
|
|
|
|
|
|
|
|
|
/* This is the hardcore match for announce*/ |
|
|
|
|
if ((*write_ptr == 'a') || (*write_ptr == '?')) |
|
|
|
@ -649,13 +722,16 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
@@ -649,13 +722,16 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
|
|
|
|
|
ws->keep_alive = 0; |
|
|
|
|
#ifdef WANT_KEEPALIVE |
|
|
|
|
read_ptr = http_header(ws->request, ws->header_size, "connection"); |
|
|
|
|
if( read_ptr && ( *read_ptr == 'K' || *read_ptr == 'k' ) ) ws->keep_alive = 1; |
|
|
|
|
if (read_ptr && (*read_ptr == 'K' || *read_ptr == 'k')) |
|
|
|
|
ws->keep_alive = 1; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/* If routines handled sending themselves, just return */ |
|
|
|
|
if( ws->reply_size == -2 ) return 0; |
|
|
|
|
if (ws->reply_size == -2) |
|
|
|
|
return 0; |
|
|
|
|
/* If routine failed, let http error take over */ |
|
|
|
|
if( ws->reply_size <= 0 ) HTTPERROR_500; |
|
|
|
|
if (ws->reply_size <= 0) |
|
|
|
|
HTTPERROR_500; |
|
|
|
|
|
|
|
|
|
/* This one is rather ugly, so I take you step by step through it.
|
|
|
|
|
|
|
|
|
|