diff options
Diffstat (limited to 'trackerlogic.h')
| -rw-r--r-- | trackerlogic.h | 178 | 
1 files changed, 108 insertions, 70 deletions
diff --git a/trackerlogic.h b/trackerlogic.h index 721ba6e..022184d 100644 --- a/trackerlogic.h +++ b/trackerlogic.h  | |||
| @@ -6,131 +6,166 @@ | |||
| 6 | #ifndef OT_TRACKERLOGIC_H__ | 6 | #ifndef OT_TRACKERLOGIC_H__ | 
| 7 | #define OT_TRACKERLOGIC_H__ | 7 | #define OT_TRACKERLOGIC_H__ | 
| 8 | 8 | ||
| 9 | #include <sys/types.h> | 9 | #include <stdint.h> | 
| 10 | #include <stdlib.h> | ||
| 10 | #include <sys/time.h> | 11 | #include <sys/time.h> | 
| 12 | #include <sys/types.h> | ||
| 11 | #include <time.h> | 13 | #include <time.h> | 
| 12 | #include <stdint.h> | 14 | |
| 15 | #if defined(__linux__) && defined(WANT_ARC4RANDOM) | ||
| 16 | #include <bsd/stdlib.h> | ||
| 17 | #endif | ||
| 18 | #ifdef __FreeBSD__ | ||
| 19 | #define WANT_ARC4RANDOM | ||
| 20 | #endif | ||
| 13 | 21 | ||
| 14 | typedef uint8_t ot_hash[20]; | 22 | typedef uint8_t ot_hash[20]; | 
| 15 | typedef time_t ot_time; | 23 | typedef time_t ot_time; | 
| 16 | typedef char ot_ip6[16]; | 24 | typedef char ot_ip6[16]; | 
| 17 | typedef struct { ot_ip6 address; int bits; } | 25 | typedef struct { | 
| 18 | ot_net; | 26 | ot_ip6 address; | 
| 19 | #ifdef WANT_V6 | 27 | int bits; | 
| 20 | #define OT_IP_SIZE 16 | 28 | } ot_net; | 
| 21 | #define PEERS_BENCODED "6:peers6" | 29 | /* List of peers should fit in a single UDP packet (around 1200 bytes) */ | 
| 22 | #else | 30 | #define OT_MAX_PEERS_UDP6 66 | 
| 23 | #define OT_IP_SIZE 4 | 31 | #define OT_MAX_PEERS_UDP4 200 | 
| 24 | #define PEERS_BENCODED "5:peers" | 32 | |
| 25 | #endif | 33 | #define OT_IP_SIZE6 16 | 
| 34 | #define OT_IP_SIZE4 4 | ||
| 35 | #define OT_PORT_SIZE 2 | ||
| 36 | #define OT_FLAG_SIZE 1 | ||
| 37 | #define OT_TIME_SIZE 1 | ||
| 26 | 38 | ||
| 27 | /* Some tracker behaviour tunable */ | 39 | /* Some tracker behaviour tunable */ | 
| 28 | #define OT_CLIENT_TIMEOUT 30 | 40 | #define OT_CLIENT_TIMEOUT 30 | 
| 29 | #define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10 | 41 | #define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10 | 
| 30 | #define OT_CLIENT_TIMEOUT_SEND (60*15) | 42 | #define OT_CLIENT_TIMEOUT_SEND (60 * 15) | 
| 31 | #define OT_CLIENT_REQUEST_INTERVAL (60*30) | 43 | #define OT_CLIENT_REQUEST_INTERVAL (60 * 30) | 
| 32 | #define OT_CLIENT_REQUEST_VARIATION (60*6) | 44 | #define OT_CLIENT_REQUEST_VARIATION (60 * 6) | 
| 33 | 45 | ||
| 34 | #define OT_TORRENT_TIMEOUT_HOURS 24 | 46 | #define OT_TORRENT_TIMEOUT_HOURS 24 | 
| 35 | #define OT_TORRENT_TIMEOUT (60*OT_TORRENT_TIMEOUT_HOURS) | 47 | #define OT_TORRENT_TIMEOUT (60 * OT_TORRENT_TIMEOUT_HOURS) | 
| 36 | 48 | ||
| 37 | #define OT_CLIENT_REQUEST_INTERVAL_RANDOM ( OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION/2 + (int)( random( ) % OT_CLIENT_REQUEST_VARIATION ) ) | 49 | #define OT_CLIENT_REQUEST_INTERVAL_RANDOM \ | 
| 50 | (OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION / 2 + (int)(nrand48(ws->rand48_state) % OT_CLIENT_REQUEST_VARIATION)) | ||
| 38 | 51 | ||
| 39 | /* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not | 52 | /* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not | 
| 40 | fullscrape more frequently than this amount in seconds */ | 53 | fullscrape more frequently than this amount in seconds */ | 
| 41 | #define OT_MODEST_PEER_TIMEOUT (60*5) | 54 | #define OT_MODEST_PEER_TIMEOUT (60 * 5) | 
| 42 | 55 | ||
| 43 | /* If peers come back before 10 minutes, don't live sync them */ | 56 | /* If peers come back before 10 minutes, don't live sync them */ | 
| 44 | #define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 | 57 | #define OT_CLIENT_SYNC_RENEW_BOUNDARY 10 | 
| 45 | 58 | ||
| 46 | /* Number of tracker admin ip addresses allowed */ | 59 | /* Number of tracker admin ip addresses allowed */ | 
| 47 | #define OT_ADMINIP_MAX 64 | 60 | #define OT_ADMINIP_MAX 64 | 
| 48 | #define OT_MAX_THREADS 64 | 61 | #define OT_MAX_THREADS 64 | 
| 49 | 62 | ||
| 50 | #define OT_PEER_TIMEOUT 45 | 63 | /* Number of minutes after announce before peer is removed */ | 
| 64 | #define OT_PEER_TIMEOUT 45 | ||
| 51 | 65 | ||
| 52 | /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs | 66 | /* We maintain a list of 1024 pointers to sorted list of ot_torrent structs | 
| 53 | Sort key is, of course, its hash */ | 67 | Sort key is, of course, its hash */ | 
| 54 | #define OT_BUCKET_COUNT_BITS 10 | 68 | #define OT_BUCKET_COUNT_BITS 10 | 
| 69 | |||
| 70 | #define OT_BUCKET_COUNT (1 << OT_BUCKET_COUNT_BITS) | ||
| 71 | #define OT_BUCKET_COUNT_SHIFT (32 - OT_BUCKET_COUNT_BITS) | ||
| 55 | 72 | ||
| 56 | #define OT_BUCKET_COUNT (1<<OT_BUCKET_COUNT_BITS) | 73 | /* if _DEBUG_RANDOMTORRENTS is set, this is the amount of torrents to create | 
| 57 | #define OT_BUCKET_COUNT_SHIFT (32-OT_BUCKET_COUNT_BITS) | 74 | on startup */ | 
| 75 | #define RANDOMTORRENTS (1024 * 1024 * 1) | ||
| 58 | 76 | ||
| 59 | /* From opentracker.c */ | 77 | /* From opentracker.c */ | 
| 60 | extern time_t g_now_seconds; | 78 | extern time_t g_now_seconds; | 
| 61 | extern volatile int g_opentracker_running; | 79 | extern volatile int g_opentracker_running; | 
| 62 | #define g_now_minutes (g_now_seconds/60) | 80 | #define g_now_minutes (g_now_seconds / 60) | 
| 63 | 81 | ||
| 64 | extern uint32_t g_tracker_id; | 82 | extern uint32_t g_tracker_id; | 
| 65 | typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; | 83 | typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG; | 
| 66 | 84 | ||
| 67 | typedef struct { | 85 | #define OT_PEER_COMPARE_SIZE6 ((OT_IP_SIZE6) + (OT_PORT_SIZE)) | 
| 68 | uint8_t data[OT_IP_SIZE+2+2]; | 86 | #define OT_PEER_COMPARE_SIZE4 ((OT_IP_SIZE4) + (OT_PORT_SIZE)) | 
| 69 | } ot_peer; | 87 | #define OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(PEER_SIZE) ((PEER_SIZE) - (OT_TIME_SIZE) - (OT_FLAG_SIZE)) | 
| 88 | |||
| 89 | #define OT_PEER_SIZE6 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE6)) | ||
| 90 | #define OT_PEER_SIZE4 ((OT_TIME_SIZE) + (OT_FLAG_SIZE) + (OT_PEER_COMPARE_SIZE4)) | ||
| 91 | |||
| 92 | typedef uint8_t ot_peer; /* Generic pointer to a v6 or v4 peer */ | ||
| 93 | typedef uint8_t ot_peer6[OT_PEER_SIZE6]; | ||
| 94 | typedef uint8_t ot_peer4[OT_PEER_SIZE4]; | ||
| 70 | static const uint8_t PEER_FLAG_SEEDING = 0x80; | 95 | static const uint8_t PEER_FLAG_SEEDING = 0x80; | 
| 71 | static const uint8_t PEER_FLAG_COMPLETED = 0x40; | 96 | static const uint8_t PEER_FLAG_COMPLETED = 0x40; | 
| 72 | static const uint8_t PEER_FLAG_STOPPED = 0x20; | 97 | static const uint8_t PEER_FLAG_STOPPED = 0x20; | 
| 73 | static const uint8_t PEER_FLAG_FROM_SYNC = 0x10; | 98 | static const uint8_t PEER_FLAG_FROM_SYNC = 0x10; | 
| 74 | static const uint8_t PEER_FLAG_LEECHING = 0x00; | 99 | static const uint8_t PEER_FLAG_LEECHING = 0x00; | 
| 75 | 100 | ||
| 76 | #ifdef WANT_V6 | 101 | /* Takes an ot_peer6 and returns the proper pointer to the peer and sets peer_size */ | 
| 77 | #define OT_SETIP(peer,ip) memcpy((peer),(ip),(OT_IP_SIZE)) | 102 | ot_peer *peer_from_peer6(ot_peer6 *peer, size_t *peer_size); | 
| 78 | #else | 103 | size_t peer_size_from_peer6(ot_peer6 *peer); | 
| 79 | #define OT_SETIP(peer,ip) memcpy((peer),(((uint8_t*)ip)+12),(OT_IP_SIZE)) | 104 | |
| 80 | #endif | 105 | /* New style */ | 
| 81 | #define OT_SETPORT(peer,port) memcpy(((uint8_t*)(peer))+(OT_IP_SIZE),(port),2) | 106 | #define OT_SETIP(peer, ip) memcpy((uint8_t *)(peer), (ip), OT_IP_SIZE6) | 
| 82 | #define OT_PEERFLAG(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+2]) | 107 | #define OT_SETPORT(peer, port) memcpy(((uint8_t *)(peer)) + (OT_IP_SIZE6), (port), 2) | 
| 83 | #define OT_PEERTIME(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+3]) | 108 | #define OT_PEERFLAG(peer) (((uint8_t *)(peer))[(OT_IP_SIZE6) + 2]) | 
| 109 | #define OT_PEERFLAG_D(peer, peersize) (((uint8_t *)(peer))[(peersize) - 2]) | ||
| 110 | #define OT_PEERTIME(peer, peersize) (((uint8_t *)(peer))[(peersize) - 1]) | ||
| 111 | |||
| 112 | #define PEERS_BENCODED6 "6:peers6" | ||
| 113 | #define PEERS_BENCODED4 "5:peers" | ||
| 84 | 114 | ||
| 85 | #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) | 115 | #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) | 
| 86 | #define OT_PEER_COMPARE_SIZE ((OT_IP_SIZE)+2) | ||
| 87 | 116 | ||
| 88 | struct ot_peerlist; | 117 | struct ot_peerlist; | 
| 89 | typedef struct ot_peerlist ot_peerlist; | 118 | typedef struct ot_peerlist ot_peerlist; | 
| 90 | typedef struct { | 119 | typedef struct { | 
| 91 | ot_hash hash; | 120 | ot_hash hash; | 
| 92 | ot_peerlist *peer_list; | 121 | ot_peerlist *peer_list6; | 
| 122 | ot_peerlist *peer_list4; | ||
| 93 | } ot_torrent; | 123 | } ot_torrent; | 
| 94 | 124 | ||
| 95 | #include "ot_vector.h" | 125 | #include "ot_vector.h" | 
| 96 | 126 | ||
| 97 | struct ot_peerlist { | 127 | struct ot_peerlist { | 
| 98 | ot_time base; | 128 | ot_time base; | 
| 99 | size_t seed_count; | 129 | size_t seed_count; | 
| 100 | size_t peer_count; | 130 | size_t peer_count; | 
| 101 | size_t down_count; | 131 | size_t down_count; | 
| 102 | /* normal peers vector or | 132 | /* normal peers vector or | 
| 103 | pointer to ot_vector[32] buckets if data != NULL and space == 0 | 133 | pointer to ot_vector[32] buckets if data != NULL and space == 0 | 
| 104 | */ | 134 | */ | 
| 105 | ot_vector peers; | 135 | ot_vector peers; | 
| 106 | }; | 136 | }; | 
| 107 | #define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space) | 137 | #define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space) | 
| 108 | 138 | ||
| 109 | struct ot_workstruct { | 139 | struct ot_workstruct { | 
| 110 | /* Thread specific, static */ | 140 | /* Thread specific, static */ | 
| 111 | char *inbuf; | 141 | char *inbuf; | 
| 112 | #define G_INBUF_SIZE 8192 | 142 | #define G_INBUF_SIZE 8192 | 
| 113 | char *outbuf; | 143 | char *outbuf; | 
| 114 | #define G_OUTBUF_SIZE 8192 | 144 | #define G_OUTBUF_SIZE 8192 | 
| 115 | #ifdef _DEBUG_HTTPERROR | 145 | #ifdef _DEBUG_HTTPERROR | 
| 116 | char *debugbuf; | 146 | char *debugbuf; | 
| 117 | #define G_DEBUGBUF_SIZE 8192 | 147 | #define G_DEBUGBUF_SIZE 8192 | 
| 118 | #endif | 148 | #endif | 
| 119 | 149 | ||
| 120 | /* The peer currently in the working */ | 150 | /* The peer currently in the working */ | 
| 121 | ot_peer peer; | 151 | ot_peer6 peer; /* Can fit v6 and v4 peers */ | 
| 122 | 152 | ||
| 123 | /* Pointers into the request buffer */ | 153 | /* Pointers into the request buffer */ | 
| 124 | ot_hash *hash; | 154 | ot_hash *hash; | 
| 125 | char *peer_id; | 155 | char *peer_id; | 
| 126 | 156 | ||
| 127 | /* HTTP specific, non static */ | 157 | /* HTTP specific, non static */ | 
| 128 | int keep_alive; | ||
| 129 | char *request; | 158 | char *request; | 
| 130 | ssize_t request_size; | 159 | ssize_t request_size; | 
| 131 | ssize_t header_size; | 160 | ssize_t header_size; | 
| 132 | char *reply; | 161 | char *reply; | 
| 133 | ssize_t reply_size; | 162 | ssize_t reply_size; | 
| 163 | |||
| 164 | /* Entropy state for rand48 function so that threads don't need to acquire mutexes for | ||
| 165 | global random() or arc4random() state, which causes heavy load on linuxes */ | ||
| 166 | uint16_t rand48_state[3]; | ||
| 167 | |||
| 168 | int keep_alive; | ||
| 134 | }; | 169 | }; | 
| 135 | 170 | ||
| 136 | /* | 171 | /* | 
| @@ -142,31 +177,34 @@ struct ot_workstruct { | |||
| 142 | #endif | 177 | #endif | 
| 143 | 178 | ||
| 144 | #ifdef WANT_SYNC | 179 | #ifdef WANT_SYNC | 
| 145 | #define WANT_SYNC_PARAM( param ) , param | 180 | #define WANT_SYNC_PARAM(param) , param | 
| 146 | #else | 181 | #else | 
| 147 | #define WANT_SYNC_PARAM( param ) | 182 | #define WANT_SYNC_PARAM(param) | 
| 148 | #endif | 183 | #endif | 
| 149 | 184 | ||
| 150 | #ifdef WANT_LOG_NETWORKS | 185 | #ifdef WANT_LOG_NETWORKS | 
| 151 | #error Live logging networks disabled at the moment. | 186 | #error Live logging networks disabled at the moment. | 
| 152 | #endif | 187 | #endif | 
| 153 | 188 | ||
| 154 | void trackerlogic_init( ); | 189 | void trackerlogic_init(void); | 
| 155 | void trackerlogic_deinit( void ); | 190 | void trackerlogic_deinit(void); | 
| 156 | void exerr( char * message ); | 191 | void exerr(char *message); | 
| 157 | 192 | ||
| 158 | /* add_peer_to_torrent does only release the torrent bucket if from_sync is set, | 193 | /* add_peer_to_torrent does only release the torrent bucket if from_sync is set, | 
| 159 | otherwise it is released in return_peers_for_torrent */ | 194 | otherwise it is released in return_peers_for_torrent */ | 
| 160 | size_t add_peer_to_torrent_and_return_peers( PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount ); | 195 | size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount); | 
| 161 | size_t remove_peer_from_torrent( PROTO_FLAG proto, struct ot_workstruct *ws ); | 196 | size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws); | 
| 162 | size_t return_tcp_scrape_for_torrent( ot_hash *hash, int amount, char *reply ); | 197 | size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, int amount, char *reply); | 
| 163 | size_t return_udp_scrape_for_torrent( ot_hash hash, char *reply ); | 198 | size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply); | 
| 164 | void add_torrent_from_saved_state( ot_hash hash, ot_time base, size_t down_count ); | 199 | void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_count); | 
| 200 | #ifdef _DEBUG_RANDOMTORRENTS | ||
| 201 | void trackerlogic_add_random_torrents(size_t amount); | ||
| 202 | #endif | ||
| 165 | 203 | ||
| 166 | /* torrent iterator */ | 204 | /* torrent iterator */ | 
| 167 | void iterate_all_torrents( int (*for_each)( ot_torrent* torrent, uintptr_t data ), uintptr_t data ); | 205 | void iterate_all_torrents(int (*for_each)(ot_torrent *torrent, uintptr_t data), uintptr_t data); | 
| 168 | 206 | ||
| 169 | /* Helper, before it moves to its own object */ | 207 | /* Helper, before it moves to its own object */ | 
| 170 | void free_peerlist( ot_peerlist *peer_list ); | 208 | void free_peerlist(ot_peerlist *peer_list); | 
| 171 | 209 | ||
| 172 | #endif | 210 | #endif | 
