-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackerlogic.h
191 lines (153 loc) · 5.59 KB
/
trackerlogic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* This software was written by Dirk Engling <[email protected]>
It is considered beerware. Prost. Skol. Cheers or whatever.
$id$ */
#ifndef __OT_TRACKERLOGIC_H__
#define __OT_TRACKERLOGIC_H__
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
typedef uint8_t ot_hash[20];
typedef time_t ot_time;
typedef char ot_ip6[16];
typedef struct { ot_ip6 address; int bits; }
ot_net;
#ifdef WANT_V6
#define OT_IP_SIZE 16
#define PEERS_BENCODED "6:peers6"
#else
#define OT_IP_SIZE 4
#define PEERS_BENCODED "5:peers"
#endif
/* Some tracker behaviour tunable */
//P2P File Replication Donnet Stéphane
//Here might be the client time out settings
#define OT_CLIENT_TIMEOUT 30 //DS File replication Change from 30 to 1
#define OT_CLIENT_TIMEOUT_CHECKINTERVAL 10 //DS File replication Change from 10 to 1
#define OT_CLIENT_TIMEOUT_SEND (60*15)
#define OT_CLIENT_REQUEST_INTERVAL 6*30 //DS File replication Change from 60*30 to 1
#define OT_CLIENT_REQUEST_VARIATION 6*6 //DS File replication Change from 60*6 to 1
#define OT_TORRENT_TIMEOUT_HOURS 24
#define OT_TORRENT_TIMEOUT (60*OT_TORRENT_TIMEOUT_HOURS)
#define OT_CLIENT_REQUEST_INTERVAL_RANDOM ( OT_CLIENT_REQUEST_INTERVAL - OT_CLIENT_REQUEST_VARIATION/2 + (int)( random( ) % OT_CLIENT_REQUEST_VARIATION ) )
/* If WANT_MODEST_FULLSCRAPES is on, ip addresses may not
fullscrape more frequently than this amount in seconds */
#define OT_MODEST_PEER_TIMEOUT (60*5)
/* If peers come back before 10 minutes, don't live sync them */
#define OT_CLIENT_SYNC_RENEW_BOUNDARY 10
/* Number of tracker admin ip addresses allowed */
#define OT_ADMINIP_MAX 64
#define OT_MAX_THREADS 16
#define OT_PEER_TIMEOUT 45
/* We maintain a list of 1024 pointers to sorted list of ot_torrent structs
Sort key is, of course, its hash */
#define OT_BUCKET_COUNT_BITS 10
#define OT_BUCKET_COUNT (1<<OT_BUCKET_COUNT_BITS)
#define OT_BUCKET_COUNT_SHIFT (32-OT_BUCKET_COUNT_BITS)
#define SHA_DIGEST_LENGTH 20 //ADDED for file replication
/* From opentracker.c */
extern time_t g_now_seconds;
extern volatile int g_opentracker_running;
#define g_now_minutes (g_now_seconds/60)
extern uint32_t g_tracker_id;
typedef enum { FLAG_TCP, FLAG_UDP, FLAG_MCA, FLAG_SELFPIPE } PROTO_FLAG;
typedef struct {
uint8_t data[OT_IP_SIZE+2+2];
} ot_peer;
static const uint8_t PEER_FLAG_SEEDING = 0x80;
static const uint8_t PEER_FLAG_COMPLETED = 0x40;
static const uint8_t PEER_FLAG_STOPPED = 0x20;
static const uint8_t PEER_FLAG_FROM_SYNC = 0x10;
static const uint8_t PEER_FLAG_LEECHING = 0x00;
#ifdef WANT_V6
#define OT_SETIP(peer,ip) memcpy((peer),(ip),(OT_IP_SIZE))
#else
#define OT_SETIP(peer,ip) memcpy((peer),(((uint8_t*)ip)+12),(OT_IP_SIZE))
#endif
#define OT_SETPORT(peer,port) memcpy(((uint8_t*)(peer))+(OT_IP_SIZE),(port),2)
#define OT_PEERFLAG(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+2])
#define OT_PEERTIME(peer) (((uint8_t*)(peer))[(OT_IP_SIZE)+3])
#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash))
#define OT_PEER_COMPARE_SIZE ((OT_IP_SIZE)+2)
struct ot_peerlist;
typedef struct ot_peerlist ot_peerlist;
typedef struct {
ot_hash hash;
ot_peerlist *peer_list;
/* ADDED for file replication */
unsigned int pieceSize;
unsigned int pieceCount;
unsigned int totalSize;
unsigned int nextPiece;
/* pieces raw sha */
char* piecesRawSign;
}ot_torrent;
#include "ot_vector.h"
struct ot_peerlist {
ot_time base;
size_t seed_count;
size_t peer_count;
size_t down_count;
/* normal peers vector or
pointer to ot_vector[32] buckets if data != NULL and space == 0
*/
ot_vector peers;
};
#define OT_PEERLIST_HASBUCKETS(peer_list) ((peer_list)->peers.size > (peer_list)->peers.space)
struct ot_workstruct {
/* Thread specific, static */
char *inbuf;
#define G_INBUF_SIZE 8192
char *outbuf;
#define G_OUTBUF_SIZE 8192
#ifdef _DEBUG_HTTPERROR
char *debugbuf;
#define G_DEBUGBUF_SIZE 8192
#endif
/* The peer currently in the working */
ot_peer peer;
/* Pointers into the request buffer */
ot_hash *hash;
char *peer_id;
/* HTTP specific, non static */
int keep_alive;
char *request;
ssize_t request_size;
ssize_t header_size;
char *reply;
ssize_t reply_size;
unsigned int pieceSize;
unsigned int pieceCount;
unsigned int totalSize;
/* pieces raw sha */
char* piecesRawSign;
};
/*
Exported functions
*/
#ifdef WANT_SYNC_LIVE
#define WANT_SYNC
#endif
#ifdef WANT_SYNC
#define WANT_SYNC_PARAM( param ) , param
#else
#define WANT_SYNC_PARAM( param )
#endif
#ifdef WANT_LOG_NETWORKS
#error Live logging networks disabled at the moment.
#endif
void trackerlogic_init( );
void trackerlogic_deinit( void );
void exerr( char * message );
/* add_peer_to_torrent does only release the torrent bucket if from_sync is set,
otherwise it is released in return_peers_for_torrent */
size_t add_peer_to_torrent_and_return_peers( PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount );
size_t remove_peer_from_torrent( PROTO_FLAG proto, struct ot_workstruct *ws );
size_t return_tcp_scrape_for_torrent( ot_hash *hash, int amount, char *reply );
size_t return_udp_scrape_for_torrent( ot_hash hash, char *reply );
void add_torrent_from_saved_state( ot_hash hash, ot_time base, size_t down_count );
/* torrent iterator */
void iterate_all_torrents( int (*for_each)( ot_torrent* torrent, uintptr_t data ), uintptr_t data );
/* Helper, before it moves to its own object */
void free_peerlist( ot_peerlist *peer_list );
#endif