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.
 
 
 
 

173 lines
4.8 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. #ifndef OT_TRACKERLOGIC_H__
  5. #define OT_TRACKERLOGIC_H__
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include <time.h>
  9. #include <stdint.h>
  10. typedef uint8_t ot_hash[20];
  11. typedef time_t ot_time;
  12. typedef char ot_ip6[16];
  13. typedef struct { ot_ip6 address; int bits; }
  14. ot_net;
  15. #ifdef WANT_V6
  16. #define OT_IP_SIZE 16
  17. #define PEERS_BENCODED "6:peers6"
  18. #else
  19. #define OT_IP_SIZE 4
  20. #define PEERS_BENCODED "5:peers"
  21. #endif
  22. /* Some tracker behaviour tunable */
  23. #define OT_CLIENT_TIMEOUT 30
  24. #define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10
  25. #define OT_CLIENT_TIMEOUT_SEND (60*15)
  26. #define OT_CLIENT_REQUEST_INTERVAL (60*30)
  27. #define OT_CLIENT_REQUEST_VARIATION (60*6)
  28. #define OT_TORRENT_TIMEOUT_HOURS 24
  29. #define OT_TORRENT_TIMEOUT (60*OT_TORRENT_TIMEOUT_HOURS)
  30. #define OT_CLIENT_REQUEST_INTERVAL_RANDOM ( OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION/2 + (int)( random( ) % OT_CLIENT_REQUEST_VARIATION ) )
  31. /* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not
  32. fullscrape more frequently than this amount in seconds */
  33. #define OT_MODEST_PEER_TIMEOUT (60*5)
  34. /* If peers come back before 10 minutes, don't live sync them */
  35. #define OT_CLIENT_SYNC_RENEW_BOUNDARY 10
  36. /* Number of tracker admin ip addresses allowed */
  37. #define OT_ADMINIP_MAX 64
  38. #define OT_MAX_THREADS 64
  39. #define OT_PEER_TIMEOUT 45
  40. /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs
  41. Sort key is, of course, its hash */
  42. #define OT_BUCKET_COUNT_BITS 10
  43. #define OT_BUCKET_COUNT (1<<OT_BUCKET_COUNT_BITS)
  44. #define OT_BUCKET_COUNT_SHIFT (32-OT_BUCKET_COUNT_BITS)
  45. /* From opentracker.c */
  46. extern time_t g_now_seconds;
  47. extern volatile int g_opentracker_running;
  48. #define g_now_minutes (g_now_seconds/60)
  49. extern uint32_t g_tracker_id;
  50. typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG;
  51. typedef struct {
  52. uint8_t data[OT_IP_SIZE+2+2];
  53. } ot_peer;
  54. static const uint8_t PEER_FLAG_SEEDING = 0x80;
  55. static const uint8_t PEER_FLAG_COMPLETED = 0x40;
  56. static const uint8_t PEER_FLAG_STOPPED = 0x20;
  57. static const uint8_t PEER_FLAG_FROM_SYNC = 0x10;
  58. static const uint8_t PEER_FLAG_LEECHING = 0x00;
  59. #ifdef WANT_V6
  60. #define OT_SETIP(peer,ip) memcpy((peer),(ip),(OT_IP_SIZE))
  61. #else
  62. #define OT_SETIP(peer,ip) memcpy((peer),(((uint8_t*)ip)+12),(OT_IP_SIZE))
  63. #endif
  64. #define OT_SETPORT(peer,port) memcpy(((uint8_t*)(peer))+(OT_IP_SIZE),(port),2)
  65. #define OT_PEERFLAG(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+2])
  66. #define OT_PEERTIME(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+3])
  67. #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash))
  68. #define OT_PEER_COMPARE_SIZE ((OT_IP_SIZE)+2)
  69. struct ot_peerlist;
  70. typedef struct ot_peerlist ot_peerlist;
  71. typedef struct {
  72. ot_hash hash;
  73. ot_peerlist *peer_list;
  74. } ot_torrent;
  75. #include "ot_vector.h"
  76. struct ot_peerlist {
  77. ot_time base;
  78. size_t seed_count;
  79. size_t peer_count;
  80. size_t down_count;
  81. /* normal peers vector or
  82. pointer to ot_vector[32] buckets if data != NULL and space == 0
  83. */
  84. ot_vector peers;
  85. };
  86. #define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space)
  87. struct ot_workstruct {
  88. /* Thread specific, static */
  89. char *inbuf;
  90. #define G_INBUF_SIZE 8192
  91. char *outbuf;
  92. #define G_OUTBUF_SIZE 8192
  93. #ifdef _DEBUG_HTTPERROR
  94. char *debugbuf;
  95. #define G_DEBUGBUF_SIZE 8192
  96. #endif
  97. /* The peer currently in the working */
  98. ot_peer peer;
  99. /* Pointers into the request buffer */
  100. ot_hash *hash;
  101. char *peer_id;
  102. /* HTTP specific, non static */
  103. int keep_alive;
  104. char *request;
  105. ssize_t request_size;
  106. ssize_t header_size;
  107. char *reply;
  108. ssize_t reply_size;
  109. };
  110. /*
  111. Exported functions
  112. */
  113. #ifdef WANT_SYNC_LIVE
  114. #define WANT_SYNC
  115. #endif
  116. #ifdef WANT_SYNC
  117. #define WANT_SYNC_PARAM( param ) , param
  118. #else
  119. #define WANT_SYNC_PARAM( param )
  120. #endif
  121. #ifdef WANT_LOG_NETWORKS
  122. #error Live logging networks disabled at the moment.
  123. #endif
  124. void trackerlogic_init( );
  125. void trackerlogic_deinit( void );
  126. void exerr( char * message );
  127. /* add_peer_to_torrent does only release the torrent bucket if from_sync is set,
  128. otherwise it is released in return_peers_for_torrent */
  129. size_t add_peer_to_torrent_and_return_peers( PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount );
  130. size_t remove_peer_from_torrent( PROTO_FLAG proto, struct ot_workstruct *ws );
  131. size_t return_tcp_scrape_for_torrent( ot_hash *hash, int amount, char *reply );
  132. size_t return_udp_scrape_for_torrent( ot_hash hash, char *reply );
  133. void add_torrent_from_saved_state( ot_hash hash, ot_time base, size_t down_count );
  134. /* torrent iterator */
  135. void iterate_all_torrents( int (*for_each)( ot_torrent* torrent, uintptr_t data ), uintptr_t data );
  136. /* Helper, before it moves to its own object */
  137. void free_peerlist( ot_peerlist *peer_list );
  138. #endif