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