An open and free bittorrent tracker https://erdgeist.org/gitweb/opentracker
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

636 wiersze
23 KiB

  1. /* This software was written by Dirk Engling <erdgeist@erdgeist.org>
  2. It is considered beerware. Prost. Skol. Cheers or whatever.
  3. $id$ */
  4. /* System */
  5. #include <sys/types.h>
  6. #include <arpa/inet.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <pthread.h>
  12. /* Libowfat */
  13. #include "byte.h"
  14. #include "array.h"
  15. #include "iob.h"
  16. #include "ip6.h"
  17. #include "scan.h"
  18. #include "case.h"
  19. /* Opentracker */
  20. #include "trackerlogic.h"
  21. #include "ot_mutex.h"
  22. #include "ot_http.h"
  23. #include "ot_iovec.h"
  24. #include "scan_urlencoded_query.h"
  25. #include "ot_fullscrape.h"
  26. #include "ot_stats.h"
  27. #include "ot_accesslist.h"
  28. #define OT_MAXMULTISCRAPE_COUNT 64
  29. extern char *g_redirecturl;
  30. char *g_stats_path;
  31. ssize_t g_stats_path_len;
  32. enum {
  33. SUCCESS_HTTP_HEADER_LENGTH = 80,
  34. SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING = 32,
  35. SUCCESS_HTTP_SIZE_OFF = 17 };
  36. static void http_senddata( const int64 sock, struct ot_workstruct *ws ) {
  37. struct http_data *cookie = io_getcookie( sock );
  38. ssize_t written_size;
  39. if( !cookie ) { io_close(sock); return; }
  40. /* whoever sends data is not interested in its input-array */
  41. if( ws->keep_alive && ws->header_size != ws->request_size ) {
  42. size_t rest = ws->request_size - ws->header_size;
  43. if( array_start(&cookie->request) ) {
  44. memmove( array_start(&cookie->request), ws->request + ws->header_size, rest );
  45. array_truncate( &cookie->request, 1, rest );
  46. } else
  47. array_catb(&cookie->request, ws->request + ws->header_size, rest );
  48. } else
  49. array_reset( &cookie->request );
  50. written_size = write( sock, ws->reply, ws->reply_size );
  51. if( ( written_size < 0 ) || ( ( written_size == ws->reply_size ) && !ws->keep_alive ) ) {
  52. array_reset( &cookie->request );
  53. free( cookie ); io_close( sock ); return;
  54. }
  55. if( written_size < ws->reply_size ) {
  56. char * outbuf;
  57. tai6464 t;
  58. if( !( outbuf = malloc( ws->reply_size - written_size ) ) ) {
  59. array_reset( &cookie->request );
  60. free(cookie); io_close( sock );
  61. return;
  62. }
  63. memcpy( outbuf, ws->reply + written_size, ws->reply_size - written_size );
  64. iob_addbuf_free( &cookie->batch, outbuf, ws->reply_size - written_size );
  65. /* writeable short data sockets just have a tcp timeout */
  66. if( !ws->keep_alive ) {
  67. taia_uint( &t, 0 ); io_timeout( sock, t );
  68. io_dontwantread( sock );
  69. }
  70. io_wantwrite( sock );
  71. }
  72. }
  73. #define HTTPERROR_302 return http_issue_error( sock, ws, CODE_HTTPERROR_302 )
  74. #define HTTPERROR_400 return http_issue_error( sock, ws, CODE_HTTPERROR_400 )
  75. #define HTTPERROR_400_PARAM return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM )
  76. #define HTTPERROR_400_COMPACT return http_issue_error( sock, ws, CODE_HTTPERROR_400_COMPACT )
  77. #define HTTPERROR_400_DOUBLEHASH return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM )
  78. #define HTTPERROR_402_NOTMODEST return http_issue_error( sock, ws, CODE_HTTPERROR_402_NOTMODEST )
  79. #define HTTPERROR_403_IP return http_issue_error( sock, ws, CODE_HTTPERROR_403_IP )
  80. #define HTTPERROR_404 return http_issue_error( sock, ws, CODE_HTTPERROR_404 )
  81. #define HTTPERROR_500 return http_issue_error( sock, ws, CODE_HTTPERROR_500 )
  82. ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code ) {
  83. char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required",
  84. "403 Not Modest", "403 Access Denied", "404 Not Found", "500 Internal Server Error" };
  85. char *title = error_code[code];
  86. ws->reply = ws->outbuf;
  87. if( code == CODE_HTTPERROR_302 )
  88. 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 );
  89. else
  90. 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);
  91. #ifdef _DEBUG_HTTPERROR
  92. fprintf( stderr, "DEBUG: invalid request was: %s\n", ws->debugbuf );
  93. #endif
  94. stats_issue_event( EVENT_FAILED, FLAG_TCP, code );
  95. http_senddata( sock, ws );
  96. return ws->reply_size = -2;
  97. }
  98. ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iovec_entries, struct iovec *iovector ) {
  99. struct http_data *cookie = io_getcookie( sock );
  100. char *header;
  101. int i;
  102. size_t header_size, size = iovec_length( &iovec_entries, &iovector );
  103. tai6464 t;
  104. /* No cookie? Bad socket. Leave. */
  105. if( !cookie ) {
  106. iovec_free( &iovec_entries, &iovector );
  107. HTTPERROR_500;
  108. }
  109. /* If this socket collected request in a buffer, free it now */
  110. array_reset( &cookie->request );
  111. /* If we came here, wait for the answer is over */
  112. cookie->flag &= ~STRUCT_HTTP_FLAG_WAITINGFORTASK;
  113. /* Our answers never are 0 vectors. Return an error. */
  114. if( !iovec_entries ) {
  115. HTTPERROR_500;
  116. }
  117. /* Prepare space for http header */
  118. header = malloc( SUCCESS_HTTP_HEADER_LENGTH + SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING );
  119. if( !header ) {
  120. iovec_free( &iovec_entries, &iovector );
  121. HTTPERROR_500;
  122. }
  123. if( cookie->flag & STRUCT_HTTP_FLAG_GZIP )
  124. header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Encoding: gzip\r\nContent-Length: %zd\r\n\r\n", size );
  125. else if( cookie->flag & STRUCT_HTTP_FLAG_BZIP2 )
  126. header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Encoding: bzip2\r\nContent-Length: %zd\r\n\r\n", size );
  127. else
  128. header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n", size );
  129. iob_reset( &cookie->batch );
  130. iob_addbuf_free( &cookie->batch, header, header_size );
  131. /* Will move to ot_iovec.c */
  132. for( i=0; i<iovec_entries; ++i )
  133. iob_addbuf_munmap( &cookie->batch, iovector[i].iov_base, iovector[i].iov_len );
  134. free( iovector );
  135. /* writeable sockets timeout after 10 minutes */
  136. taia_now( &t ); taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_SEND );
  137. io_timeout( sock, t );
  138. io_dontwantread( sock );
  139. io_wantwrite( sock );
  140. return 0;
  141. }
  142. static ssize_t http_handle_stats( const int64 sock, struct ot_workstruct *ws, char *read_ptr ) {
  143. static const ot_keywords keywords_main[] =
  144. { { "mode", 1 }, {"format", 2 }, { NULL, -3 } };
  145. static const ot_keywords keywords_mode[] =
  146. { { "peer", TASK_STATS_PEERS }, { "conn", TASK_STATS_CONNS }, { "scrp", TASK_STATS_SCRAPE }, { "udp4", TASK_STATS_UDP }, { "tcp4", TASK_STATS_TCP },
  147. { "busy", TASK_STATS_BUSY_NETWORKS }, { "torr", TASK_STATS_TORRENTS }, { "fscr", TASK_STATS_FULLSCRAPE },
  148. { "s24s", TASK_STATS_SLASH24S }, { "tpbs", TASK_STATS_TPB }, { "herr", TASK_STATS_HTTPERRORS }, { "completed", TASK_STATS_COMPLETED },
  149. { "top100", TASK_STATS_TOP100 }, { "top10", TASK_STATS_TOP10 }, { "renew", TASK_STATS_RENEW }, { "syncs", TASK_STATS_SYNCS }, { "version", TASK_STATS_VERSION },
  150. { "everything", TASK_STATS_EVERYTHING }, { "statedump", TASK_FULLSCRAPE_TRACKERSTATE }, { "fulllog", TASK_STATS_FULLLOG },
  151. { "woodpeckers", TASK_STATS_WOODPECKERS},
  152. #ifdef WANT_LOG_NUMWANT
  153. { "numwants", TASK_STATS_NUMWANTS},
  154. #endif
  155. { NULL, -3 } };
  156. static const ot_keywords keywords_format[] =
  157. { { "bin", TASK_FULLSCRAPE_TPB_BINARY }, { "ben", TASK_FULLSCRAPE }, { "url", TASK_FULLSCRAPE_TPB_URLENCODED },
  158. { "txt", TASK_FULLSCRAPE_TPB_ASCII }, { "txtp", TASK_FULLSCRAPE_TPB_ASCII_PLUS }, { NULL, -3 } };
  159. int mode = TASK_STATS_PEERS, scanon = 1, format = 0;
  160. #ifdef WANT_RESTRICT_STATS
  161. struct http_data *cookie = io_getcookie( sock );
  162. if( !cookie || !accesslist_isblessed( cookie->ip, OT_PERMISSION_MAY_STAT ) )
  163. HTTPERROR_403_IP;
  164. #endif
  165. while( scanon ) {
  166. switch( scan_find_keywords( keywords_main, &read_ptr, SCAN_SEARCHPATH_PARAM ) ) {
  167. case -2: scanon = 0; break; /* TERMINATOR */
  168. case -1: HTTPERROR_400_PARAM; /* PARSE ERROR */
  169. case -3: scan_urlencoded_skipvalue( &read_ptr ); break;
  170. case 1: /* matched "mode" */
  171. if( ( mode = scan_find_keywords( keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM;
  172. break;
  173. case 2: /* matched "format" */
  174. if( ( format = scan_find_keywords( keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM;
  175. break;
  176. }
  177. }
  178. #ifdef WANT_FULLSCRAPE
  179. if( mode == TASK_FULLSCRAPE_TRACKERSTATE ) {
  180. format = mode; mode = TASK_STATS_TPB;
  181. }
  182. if( mode == TASK_STATS_TPB ) {
  183. struct http_data* cookie = io_getcookie( sock );
  184. tai6464 t;
  185. #ifdef WANT_COMPRESSION_GZIP
  186. ws->request[ws->request_size] = 0;
  187. #ifdef WANT_COMPRESSION_GZIP_ALWAYS
  188. if( strstr( read_ptr - 1, "gzip" ) ) {
  189. #endif
  190. cookie->flag |= STRUCT_HTTP_FLAG_GZIP;
  191. format |= TASK_FLAG_GZIP;
  192. #ifdef WANT_COMPRESSION_GZIP_ALWAYS
  193. }
  194. #endif
  195. #endif
  196. /* Pass this task to the worker thread */
  197. cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK;
  198. /* Clients waiting for us should not easily timeout */
  199. taia_uint( &t, 0 ); io_timeout( sock, t );
  200. fullscrape_deliver( sock, format );
  201. io_dontwantread( sock );
  202. return ws->reply_size = -2;
  203. }
  204. #endif
  205. /* default format for now */
  206. if( ( mode & TASK_CLASS_MASK ) == TASK_STATS ) {
  207. tai6464 t;
  208. /* Complex stats also include expensive memory debugging tools */
  209. taia_uint( &t, 0 ); io_timeout( sock, t );
  210. stats_deliver( sock, mode );
  211. return ws->reply_size = -2;
  212. }
  213. /* Simple stats can be answerred immediately */
  214. return ws->reply_size = return_stats_for_tracker( ws->reply, mode, 0 );
  215. }
  216. #ifdef WANT_MODEST_FULLSCRAPES
  217. static pthread_mutex_t g_modest_fullscrape_mutex = PTHREAD_MUTEX_INITIALIZER;
  218. static ot_vector g_modest_fullscrape_timeouts;
  219. typedef struct { ot_ip6 ip; ot_time last_fullscrape; } ot_scrape_log;
  220. #endif
  221. #ifdef WANT_FULLSCRAPE
  222. static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *ws ) {
  223. struct http_data* cookie = io_getcookie( sock );
  224. int format = 0;
  225. tai6464 t;
  226. #ifdef WANT_MODEST_FULLSCRAPES
  227. {
  228. ot_scrape_log this_peer, *new_peer;
  229. int exactmatch;
  230. memcpy( this_peer.ip, cookie->ip, sizeof(ot_ip6));
  231. this_peer.last_fullscrape = g_now_seconds;
  232. pthread_mutex_lock(&g_modest_fullscrape_mutex);
  233. new_peer = vector_find_or_insert( &g_modest_fullscrape_timeouts, &this_peer, sizeof(ot_scrape_log), sizeof(ot_ip6), &exactmatch );
  234. if( !new_peer ) {
  235. pthread_mutex_unlock(&g_modest_fullscrape_mutex);
  236. HTTPERROR_500;
  237. }
  238. if( exactmatch && ( this_peer.last_fullscrape - new_peer->last_fullscrape ) < OT_MODEST_PEER_TIMEOUT ) {
  239. pthread_mutex_unlock(&g_modest_fullscrape_mutex);
  240. HTTPERROR_402_NOTMODEST;
  241. }
  242. memcpy( new_peer, &this_peer, sizeof(ot_scrape_log));
  243. pthread_mutex_unlock(&g_modest_fullscrape_mutex);
  244. }
  245. #endif
  246. #ifdef WANT_COMPRESSION_GZIP
  247. ws->request[ws->request_size-1] = 0;
  248. if( strstr( ws->request, "gzip" ) ) {
  249. cookie->flag |= STRUCT_HTTP_FLAG_GZIP;
  250. format = TASK_FLAG_GZIP;
  251. stats_issue_event( EVENT_FULLSCRAPE_REQUEST_GZIP, 0, (uintptr_t)cookie->ip );
  252. } else
  253. #endif
  254. stats_issue_event( EVENT_FULLSCRAPE_REQUEST, 0, (uintptr_t)cookie->ip );
  255. #ifdef _DEBUG_HTTPERROR
  256. fprintf( stderr, "%s", ws->debugbuf );
  257. #endif
  258. /* Pass this task to the worker thread */
  259. cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK;
  260. /* Clients waiting for us should not easily timeout */
  261. taia_uint( &t, 0 ); io_timeout( sock, t );
  262. fullscrape_deliver( sock, TASK_FULLSCRAPE | format );
  263. io_dontwantread( sock );
  264. return ws->reply_size = -2;
  265. }
  266. #endif
  267. static ssize_t http_handle_scrape( const int64 sock, struct ot_workstruct *ws, char *read_ptr ) {
  268. static const ot_keywords keywords_scrape[] = { { "info_hash", 1 }, { NULL, -3 } };
  269. ot_hash * multiscrape_buf = (ot_hash*)ws->request;
  270. int scanon = 1, numwant = 0;
  271. /* This is to hack around stupid clients that send "scrape ?info_hash" */
  272. if( read_ptr[-1] != '?' ) {
  273. while( ( *read_ptr != '?' ) && ( *read_ptr != '\n' ) ) ++read_ptr;
  274. if( *read_ptr == '\n' ) HTTPERROR_400_PARAM;
  275. ++read_ptr;
  276. }
  277. while( scanon ) {
  278. switch( scan_find_keywords( keywords_scrape, &read_ptr, SCAN_SEARCHPATH_PARAM ) ) {
  279. case -2: scanon = 0; break; /* TERMINATOR */
  280. default: HTTPERROR_400_PARAM; /* PARSE ERROR */
  281. case -3: scan_urlencoded_skipvalue( &read_ptr ); break;
  282. case 1: /* matched "info_hash" */
  283. /* ignore this, when we have less than 20 bytes */
  284. if( scan_urlencoded_query( &read_ptr, (char*)(multiscrape_buf + numwant++), SCAN_SEARCHPATH_VALUE ) != (ssize_t)sizeof(ot_hash) )
  285. HTTPERROR_400_PARAM;
  286. break;
  287. }
  288. }
  289. /* No info_hash found? Inform user */
  290. if( !numwant ) HTTPERROR_400_PARAM;
  291. /* Limit number of hashes to process */
  292. if( numwant > OT_MAXMULTISCRAPE_COUNT )
  293. numwant = OT_MAXMULTISCRAPE_COUNT;
  294. /* Enough for http header + whole scrape string */
  295. ws->reply_size = return_tcp_scrape_for_torrent( multiscrape_buf, numwant, ws->reply );
  296. stats_issue_event( EVENT_SCRAPE, FLAG_TCP, ws->reply_size );
  297. return ws->reply_size;
  298. }
  299. #ifdef WANT_LOG_NUMWANT
  300. unsigned long long numwants[201];
  301. #endif
  302. #if defined( WANT_KEEPALIVE ) || defined( WANT_IP_FROM_PROXY )
  303. static char* http_header( char *data, size_t byte_count, char *header ) {
  304. size_t i;
  305. long sl = strlen( header );
  306. for( i = 0; i + sl + 2 < byte_count; ++i ) {
  307. if( data[i] != '\n' || data[ i + sl + 1] != ':' ) continue;
  308. if( !case_equalb( data + i + 1, sl, header ) ) continue;
  309. data += i + sl + 2;
  310. while( *data == ' ' || *data == '\t' ) ++data;
  311. return data;
  312. }
  313. return 0;
  314. }
  315. #endif
  316. static ot_keywords keywords_announce[] = { { "port", 1 }, { "left", 2 }, { "event", 3 }, { "numwant", 4 }, { "compact", 5 }, { "compact6", 5 }, { "info_hash", 6 },
  317. #ifdef WANT_IP_FROM_QUERY_STRING
  318. { "ip", 7 },
  319. #endif
  320. #ifdef WANT_FULLLOG_NETWORKS
  321. { "lognet", 8 },
  322. #endif
  323. { "peer_id", 9 },
  324. { NULL, -3 } };
  325. static ot_keywords keywords_announce_event[] = { { "completed", 1 }, { "stopped", 2 }, { NULL, -3 } };
  326. static ssize_t http_handle_announce( const int64 sock, struct ot_workstruct *ws, char *read_ptr ) {
  327. int numwant, tmp, scanon;
  328. unsigned short port = 0;
  329. char *write_ptr;
  330. ssize_t len;
  331. struct http_data *cookie = io_getcookie( sock );
  332. /* This is to hack around stupid clients that send "announce ?info_hash" */
  333. if( read_ptr[-1] != '?' ) {
  334. while( ( *read_ptr != '?' ) && ( *read_ptr != '\n' ) ) ++read_ptr;
  335. if( *read_ptr == '\n' ) HTTPERROR_400_PARAM;
  336. ++read_ptr;
  337. }
  338. #ifdef WANT_IP_FROM_PROXY
  339. if( accesslist_isblessed( cookie->ip, OT_PERMISSION_MAY_PROXY ) ) {
  340. ot_ip6 proxied_ip;
  341. char *fwd = http_header( ws->request, ws->header_size, "x-forwarded-for" );
  342. if( fwd && scan_ip6( fwd, proxied_ip ) )
  343. OT_SETIP( &ws->peer, proxied_ip );
  344. else
  345. OT_SETIP( &ws->peer, cookie->ip );
  346. } else
  347. #endif
  348. OT_SETIP( &ws->peer, cookie->ip );
  349. ws->peer_id = NULL;
  350. ws->hash = NULL;
  351. OT_SETPORT( &ws->peer, &port );
  352. OT_PEERFLAG( &ws->peer ) = 0;
  353. numwant = 50;
  354. scanon = 1;
  355. while( scanon ) {
  356. switch( scan_find_keywords(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM ) ) {
  357. case -2: scanon = 0; break; /* TERMINATOR */
  358. case -1: HTTPERROR_400_PARAM; /* PARSE ERROR */
  359. case -3: scan_urlencoded_skipvalue( &read_ptr ); break;
  360. case 1: /* matched "port" */
  361. len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE );
  362. if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &tmp ) || ( tmp > 0xffff ) ) HTTPERROR_400_PARAM;
  363. port = htons( tmp ); OT_SETPORT( &ws->peer, &port );
  364. break;
  365. case 2: /* matched "left" */
  366. if( ( len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM;
  367. if( scan_fixed_int( write_ptr, len, &tmp ) ) tmp = 0;
  368. if( !tmp ) OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_SEEDING;
  369. break;
  370. case 3: /* matched "event" */
  371. switch( scan_find_keywords( keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE ) ) {
  372. case -1: HTTPERROR_400_PARAM;
  373. case 1: /* matched "completed" */
  374. OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_COMPLETED;
  375. break;
  376. case 2: /* matched "stopped" */
  377. OT_PEERFLAG( &ws->peer ) |= PEER_FLAG_STOPPED;
  378. break;
  379. default:
  380. break;
  381. }
  382. break;
  383. case 4: /* matched "numwant" */
  384. len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE );
  385. if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &numwant ) ) HTTPERROR_400_PARAM;
  386. if( numwant < 0 ) numwant = 50;
  387. if( numwant > 200 ) numwant = 200;
  388. break;
  389. case 5: /* matched "compact" */
  390. len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE );
  391. if( ( len <= 0 ) || scan_fixed_int( write_ptr, len, &tmp ) ) HTTPERROR_400_PARAM;
  392. if( !tmp ) HTTPERROR_400_COMPACT;
  393. break;
  394. case 6: /* matched "info_hash" */
  395. if( ws->hash ) HTTPERROR_400_DOUBLEHASH;
  396. /* ignore this, when we have less than 20 bytes */
  397. if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM;
  398. ws->hash = (ot_hash*)write_ptr;
  399. break;
  400. #ifdef WANT_IP_FROM_QUERY_STRING
  401. case 7: /* matched "ip" */
  402. {
  403. char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply+16;
  404. len = scan_urlencoded_query( &read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE );
  405. tmp_buf2[len] = 0;
  406. if( ( len <= 0 ) || !scan_ip6( tmp_buf2, tmp_buf1 ) ) HTTPERROR_400_PARAM;
  407. OT_SETIP( &ws->peer, tmp_buf1 );
  408. }
  409. break;
  410. #endif
  411. #ifdef WANT_FULLLOG_NETWORKS
  412. case 8: /* matched "lognet" */
  413. {
  414. //if( accesslist_isblessed( cookie->ip, OT_PERMISSION_MAY_STAT ) ) {
  415. char *tmp_buf = ws->reply;
  416. ot_net net;
  417. signed short parsed, bits;
  418. len = scan_urlencoded_query( &read_ptr, tmp_buf, SCAN_SEARCHPATH_VALUE );
  419. tmp_buf[len] = 0;
  420. if( len <= 0 ) HTTPERROR_400_PARAM;
  421. if( *tmp_buf == '-' ) {
  422. loglist_reset( );
  423. return ws->reply_size = sprintf( ws->reply, "Successfully removed.\n" );
  424. }
  425. parsed = scan_ip6( tmp_buf, net.address );
  426. if( !parsed ) HTTPERROR_400_PARAM;
  427. if( tmp_buf[parsed++] != '/' )
  428. bits = 128;
  429. else {
  430. parsed = scan_short( tmp_buf + parsed, &bits );
  431. if( !parsed ) HTTPERROR_400_PARAM;
  432. if( ip6_isv4mapped( net.address ) )
  433. bits += 96;
  434. }
  435. net.bits = bits;
  436. loglist_add_network( &net );
  437. return ws->reply_size = sprintf( ws->reply, "Successfully added.\n" );
  438. //}
  439. }
  440. break;
  441. #endif
  442. case 9: /* matched "peer_id" */
  443. /* ignore this, when we have less than 20 bytes */
  444. if( scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE ) != 20 ) HTTPERROR_400_PARAM;
  445. ws->peer_id = write_ptr;
  446. break;
  447. }
  448. }
  449. #ifdef WANT_LOG_NUMWANT
  450. numwants[numwant]++;
  451. #endif
  452. /* XXX DEBUG
  453. stats_issue_event( EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ws->reply );
  454. */
  455. /* Scanned whole query string */
  456. if( !ws->hash )
  457. 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" );
  458. if( OT_PEERFLAG( &ws->peer ) & PEER_FLAG_STOPPED )
  459. ws->reply_size = remove_peer_from_torrent( FLAG_TCP, ws );
  460. else
  461. ws->reply_size = add_peer_to_torrent_and_return_peers( FLAG_TCP, ws, numwant );
  462. stats_issue_event( EVENT_ANNOUNCE, FLAG_TCP, ws->reply_size);
  463. return ws->reply_size;
  464. }
  465. ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) {
  466. ssize_t reply_off, len;
  467. char *read_ptr = ws->request, *write_ptr;
  468. #ifdef WANT_FULLLOG_NETWORKS
  469. struct http_data *cookie = io_getcookie( sock );
  470. if( loglist_check_address( cookie->ip ) ) {
  471. ot_log *log = malloc( sizeof( ot_log ) );
  472. if( log ) {
  473. log->size = ws->request_size;
  474. log->data = malloc( ws->request_size );
  475. log->next = 0;
  476. log->time = g_now_seconds;
  477. memcpy( log->ip, cookie->ip, sizeof(ot_ip6));
  478. if( log->data ) {
  479. memcpy( log->data, ws->request, ws->request_size );
  480. if( !g_logchain_first )
  481. g_logchain_first = g_logchain_last = log;
  482. else {
  483. g_logchain_last->next = log;
  484. g_logchain_last = log;
  485. }
  486. } else
  487. free( log );
  488. }
  489. }
  490. #endif
  491. #ifdef _DEBUG_HTTPERROR
  492. reply_off = ws->request_size;
  493. if( ws->request_size >= G_DEBUGBUF_SIZE )
  494. reply_off = G_DEBUGBUF_SIZE - 1;
  495. memcpy( ws->debugbuf, ws->request, reply_off );
  496. ws->debugbuf[ reply_off ] = 0;
  497. #endif
  498. /* Tell subroutines where to put reply data */
  499. ws->reply = ws->outbuf + SUCCESS_HTTP_HEADER_LENGTH;
  500. /* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */
  501. if( memcmp( read_ptr, "GET /", 5) ) HTTPERROR_400;
  502. /* Skip leading '/' */
  503. for( read_ptr+=4; *read_ptr == '/'; ++read_ptr);
  504. /* Try to parse the request.
  505. In reality we abandoned requiring the url to be correct. This now
  506. only decodes url encoded characters, we check for announces and
  507. scrapes by looking for "a*" or "sc" */
  508. len = scan_urlencoded_query( &read_ptr, write_ptr = read_ptr, SCAN_PATH );
  509. /* If parsing returned an error, leave with not found */
  510. if( g_redirecturl && ( len == -2 ) ) HTTPERROR_302;
  511. if( len <= 0 ) HTTPERROR_404;
  512. /* This is the hardcore match for announce*/
  513. if( ( *write_ptr == 'a' ) || ( *write_ptr == '?' ) )
  514. http_handle_announce( sock, ws, read_ptr );
  515. #ifdef WANT_FULLSCRAPE
  516. else if( !memcmp( write_ptr, "scrape HTTP/", 12 ) )
  517. http_handle_fullscrape( sock, ws );
  518. #endif
  519. /* This is the hardcore match for scrape */
  520. else if( !memcmp( write_ptr, "sc", 2 ) )
  521. http_handle_scrape( sock, ws, read_ptr );
  522. /* All the rest is matched the standard way */
  523. else if( len == g_stats_path_len && !memcmp( write_ptr, g_stats_path, len ) )
  524. http_handle_stats( sock, ws, read_ptr );
  525. else
  526. HTTPERROR_404;
  527. /* Find out if the client wants to keep this connection alive */
  528. ws->keep_alive = 0;
  529. #ifdef WANT_KEEPALIVE
  530. read_ptr=http_header( ws->request, ws->header_size, "connection");
  531. if( read_ptr && ( *read_ptr == 'K' || *read_ptr == 'k' ) ) ws->keep_alive = 1;
  532. #endif
  533. /* If routines handled sending themselves, just return */
  534. if( ws->reply_size == -2 ) return 0;
  535. /* If routine failed, let http error take over */
  536. if( ws->reply_size <= 0 ) HTTPERROR_500;
  537. /* This one is rather ugly, so I take you step by step through it.
  538. 1. In order to avoid having two buffers, one for header and one for content, we allow all above functions from trackerlogic to
  539. write to a fixed location, leaving SUCCESS_HTTP_HEADER_LENGTH bytes in our work buffer, which is enough for the static string
  540. plus dynamic space needed to expand our Content-Length value. We reserve SUCCESS_HTTP_SIZE_OFF for its expansion and calculate
  541. the space NOT needed to expand in reply_off
  542. */
  543. reply_off = SUCCESS_HTTP_SIZE_OFF - snprintf( ws->outbuf, 0, "%zd", ws->reply_size );
  544. ws->reply = ws->outbuf + reply_off;
  545. /* 2. Now we sprintf our header so that sprintf writes its terminating '\0' exactly one byte before content starts. Complete
  546. packet size is increased by size of header plus one byte '\n', we will copy over '\0' in next step */
  547. ws->reply_size += 1 + sprintf( ws->reply, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r", ws->reply_size );
  548. /* 3. Finally we join both blocks neatly */
  549. ws->outbuf[ SUCCESS_HTTP_HEADER_LENGTH - 1 ] = '\n';
  550. http_senddata( sock, ws );
  551. return ws->reply_size;
  552. }
  553. const char *g_version_http_c = "$Source: /home/cvsroot/opentracker/ot_http.c,v $: $Revision: 1.56 $\n";