An open and free bittorrent tracker https://erdgeist.org/gitweb/opentracker
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

250 lines
8.2 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. #ifdef WANT_FULLSCRAPE
  5. /* System */
  6. #include <sys/param.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <pthread.h>
  10. #include <arpa/inet.h>
  11. #ifdef WANT_COMPRESSION_GZIP
  12. #include <zlib.h>
  13. #endif
  14. /* Libowfat */
  15. #include "byte.h"
  16. #include "io.h"
  17. #include "textcode.h"
  18. /* Opentracker */
  19. #include "trackerlogic.h"
  20. #include "ot_mutex.h"
  21. #include "ot_iovec.h"
  22. #include "ot_fullscrape.h"
  23. /* Fetch full scrape info for all torrents
  24. Full scrapes usually are huge and one does not want to
  25. allocate more memory. So lets get them in 512k units
  26. */
  27. #define OT_SCRAPE_CHUNK_SIZE (512*1024)
  28. /* "d8:completei%zde10:downloadedi%zde10:incompletei%zdee" */
  29. #define OT_SCRAPE_MAXENTRYLEN 256
  30. #ifdef WANT_COMPRESSION_GZIP
  31. #define IF_COMPRESSION( TASK ) if( mode & TASK_FLAG_GZIP ) TASK
  32. #define WANT_COMPRESSION_GZIP_PARAM( param1, param2, param3 ) , param1, param2, param3
  33. #else
  34. #define IF_COMPRESSION( TASK )
  35. #define WANT_COMPRESSION_GZIP_PARAM( param1, param2, param3 )
  36. #endif
  37. /* Forward declaration */
  38. static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode );
  39. /* Converter function from memory to human readable hex strings
  40. XXX - Duplicated from ot_stats. Needs fix. */
  41. static char*to_hex(char*d,uint8_t*s){char*m="0123456789ABCDEF";char *t=d;char*e=d+40;while(d<e){*d++=m[*s>>4];*d++=m[*s++&15];}*d=0;return t;}
  42. /* This is the entry point into this worker thread
  43. It grabs tasks from mutex_tasklist and delivers results back
  44. */
  45. static void * fullscrape_worker( void * args ) {
  46. int iovec_entries;
  47. struct iovec *iovector;
  48. (void) args;
  49. while( 1 ) {
  50. ot_tasktype tasktype = TASK_FULLSCRAPE;
  51. ot_taskid taskid = mutex_workqueue_poptask( &tasktype );
  52. fullscrape_make( &iovec_entries, &iovector, tasktype );
  53. if( mutex_workqueue_pushresult( taskid, iovec_entries, iovector ) )
  54. iovec_free( &iovec_entries, &iovector );
  55. if( !g_opentracker_running )
  56. return NULL;
  57. }
  58. return NULL;
  59. }
  60. static pthread_t thread_id;
  61. void fullscrape_init( ) {
  62. pthread_create( &thread_id, NULL, fullscrape_worker, NULL );
  63. }
  64. void fullscrape_deinit( ) {
  65. pthread_cancel( thread_id );
  66. }
  67. void fullscrape_deliver( int64 sock, ot_tasktype tasktype ) {
  68. mutex_workqueue_pushtask( sock, tasktype );
  69. }
  70. static int fullscrape_increase( int *iovec_entries, struct iovec **iovector,
  71. char **r, char **re WANT_COMPRESSION_GZIP_PARAM( z_stream *strm, ot_tasktype mode, int zaction ) ) {
  72. /* Allocate a fresh output buffer at the end of our buffers list */
  73. if( !( *r = iovec_fix_increase_or_free( iovec_entries, iovector, *r, OT_SCRAPE_CHUNK_SIZE ) ) ) {
  74. /* Deallocate gzip buffers */
  75. IF_COMPRESSION( deflateEnd(strm); )
  76. /* Release lock on current bucket and return */
  77. return -1;
  78. }
  79. /* Adjust new end of output buffer */
  80. *re = *r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN;
  81. /* When compressing, we have all the bytes in output buffer */
  82. #ifdef WANT_COMPRESSION_GZIP
  83. if( mode & TASK_FLAG_GZIP ) {
  84. int zres;
  85. *re -= OT_SCRAPE_MAXENTRYLEN;
  86. strm->next_out = (uint8_t*)*r;
  87. strm->avail_out = OT_SCRAPE_CHUNK_SIZE;
  88. zres = deflate( strm, zaction );
  89. if( ( zres < Z_OK ) && ( zres != Z_BUF_ERROR ) )
  90. fprintf( stderr, "deflate() failed while in fullscrape_increase(%d).\n", zaction );
  91. *r = (char*)strm->next_out;
  92. }
  93. #endif
  94. return 0;
  95. }
  96. static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) {
  97. int bucket;
  98. char *r, *re;
  99. #ifdef WANT_COMPRESSION_GZIP
  100. char compress_buffer[OT_SCRAPE_MAXENTRYLEN];
  101. z_stream strm;
  102. #endif
  103. /* Setup return vector... */
  104. *iovec_entries = 0;
  105. *iovector = NULL;
  106. if( !( r = iovec_increase( iovec_entries, iovector, OT_SCRAPE_CHUNK_SIZE ) ) )
  107. return;
  108. /* re points to low watermark */
  109. re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN;
  110. #ifdef WANT_COMPRESSION_GZIP
  111. if( mode & TASK_FLAG_GZIP ) {
  112. re += OT_SCRAPE_MAXENTRYLEN;
  113. byte_zero( &strm, sizeof(strm) );
  114. strm.next_in = (uint8_t*)compress_buffer;
  115. strm.next_out = (uint8_t*)r;
  116. strm.avail_out = OT_SCRAPE_CHUNK_SIZE;
  117. if( deflateInit2(&strm,7,Z_DEFLATED,31,8,Z_DEFAULT_STRATEGY) != Z_OK )
  118. fprintf( stderr, "not ok.\n" );
  119. r = compress_buffer;
  120. }
  121. #endif
  122. if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE )
  123. r += sprintf( r, "d5:filesd" );
  124. /* For each bucket... */
  125. for( bucket=0; bucket<OT_BUCKET_COUNT; ++bucket ) {
  126. /* Get exclusive access to that bucket */
  127. ot_vector *torrents_list = mutex_bucket_lock( bucket );
  128. size_t tor_offset;
  129. /* For each torrent in this bucket.. */
  130. for( tor_offset=0; tor_offset<torrents_list->size; ++tor_offset ) {
  131. /* Address torrents members */
  132. ot_peerlist *peer_list = ( ((ot_torrent*)(torrents_list->data))[tor_offset] ).peer_list;
  133. ot_hash *hash =&( ((ot_torrent*)(torrents_list->data))[tor_offset] ).hash;
  134. switch( mode & TASK_TASK_MASK ) {
  135. case TASK_FULLSCRAPE:
  136. default:
  137. /* push hash as bencoded string */
  138. *r++='2'; *r++='0'; *r++=':';
  139. memcpy( r, hash, sizeof(ot_hash) ); r += sizeof(ot_hash);
  140. /* push rest of the scrape string */
  141. r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", peer_list->seed_count, peer_list->down_count, peer_list->peer_count-peer_list->seed_count );
  142. break;
  143. case TASK_FULLSCRAPE_TPB_ASCII:
  144. to_hex( r, *hash ); r+= 2 * sizeof(ot_hash);
  145. r += sprintf( r, ":%zd:%zd\n", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count );
  146. break;
  147. case TASK_FULLSCRAPE_TPB_ASCII_PLUS:
  148. to_hex( r, *hash ); r+= 2 * sizeof(ot_hash);
  149. r += sprintf( r, ":%zd:%zd:%zd\n", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count, peer_list->down_count );
  150. break;
  151. case TASK_FULLSCRAPE_TPB_BINARY:
  152. memcpy( r, *hash, sizeof(ot_hash) ); r += sizeof(ot_hash);
  153. *(uint32_t*)(r+0) = htonl( (uint32_t) peer_list->seed_count );
  154. *(uint32_t*)(r+4) = htonl( (uint32_t)( peer_list->peer_count-peer_list->seed_count) );
  155. r+=8;
  156. break;
  157. case TASK_FULLSCRAPE_TPB_URLENCODED:
  158. r += fmt_urlencoded( r, (char *)*hash, 20 );
  159. r += sprintf( r, ":%zd:%zd\n", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count );
  160. break;
  161. case TASK_FULLSCRAPE_TRACKERSTATE:
  162. to_hex( r, *hash ); r+= 2 * sizeof(ot_hash);
  163. r += sprintf( r, ":%zd:%zd\n", peer_list->base, peer_list->down_count );
  164. break;
  165. }
  166. #ifdef WANT_COMPRESSION_GZIP
  167. if( mode & TASK_FLAG_GZIP ) {
  168. int zres;
  169. strm.next_in = (uint8_t*)compress_buffer;
  170. strm.avail_in = r - compress_buffer;
  171. zres = deflate( &strm, Z_NO_FLUSH );
  172. if( ( zres < Z_OK ) && ( zres != Z_BUF_ERROR ) )
  173. fprintf( stderr, "deflate() failed while in fullscrape_make().\n" );
  174. r = (char*)strm.next_out;
  175. }
  176. #endif
  177. /* Check if there still is enough buffer left */
  178. while( r >= re )
  179. if( fullscrape_increase( iovec_entries, iovector, &r, &re WANT_COMPRESSION_GZIP_PARAM( &strm, mode, Z_NO_FLUSH ) ) )
  180. return mutex_bucket_unlock( bucket, 0 );
  181. IF_COMPRESSION( r = compress_buffer; )
  182. }
  183. /* All torrents done: release lock on current bucket */
  184. mutex_bucket_unlock( bucket, 0 );
  185. /* Parent thread died? */
  186. if( !g_opentracker_running )
  187. return;
  188. }
  189. if( ( mode & TASK_TASK_MASK ) == TASK_FULLSCRAPE )
  190. r += sprintf( r, "ee" );
  191. #ifdef WANT_COMPRESSION_GZIP
  192. if( mode & TASK_FLAG_GZIP ) {
  193. strm.next_in = (uint8_t*)compress_buffer;
  194. strm.avail_in = r - compress_buffer;
  195. if( deflate( &strm, Z_FINISH ) < Z_OK )
  196. fprintf( stderr, "deflate() failed while in fullscrape_make()'s endgame.\n" );
  197. r = (char*)strm.next_out;
  198. while( r >= re )
  199. if( fullscrape_increase( iovec_entries, iovector, &r, &re WANT_COMPRESSION_GZIP_PARAM( &strm, mode, Z_FINISH ) ) )
  200. return mutex_bucket_unlock( bucket, 0 );
  201. deflateEnd(&strm);
  202. }
  203. #endif
  204. /* Release unused memory in current output buffer */
  205. iovec_fixlast( iovec_entries, iovector, r );
  206. }
  207. #endif
  208. const char *g_version_fullscrape_c = "$Source: /home/cvsroot/opentracker/ot_fullscrape.c,v $: $Revision: 1.37 $\n";