-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracker.h
More file actions
212 lines (169 loc) · 5.08 KB
/
Tracker.h
File metadata and controls
212 lines (169 loc) · 5.08 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#pragma once
#include "Utils.h"
#include <stdint.h>
#define ACTION_CONNECT 0
#define ACTION_ANNOUNCE 1
#define ACTION_SCRAPE 2
#define ACTION_ERROR 3
#define EVENT_NONE 0
#define EVENT_COMPLATED 1
#define EVENT_STARTED 2
#define EVENT_STOPPED 3
#define OK 0
#define ERROR_UNKNOWN_PROTOCOL 1
#define ERROR_OUT_OF_MEMORY 2
#define RESOURCE_PEERS_TABLE_SIZE 200
//////////////////////////////////////////////////////////
//// Struct - Protocol
//////////////////////////////////////////////////////////
//static InfoHash ZERO_INFOHASH = { 0 };
#pragma pack(push, 1)
typedef union HashInfo_u {
char data[20];
struct
{
uint32_t key;
uint64_t pos;
uint64_t offset;
};
} InfoHash;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct TrackerRequest_t {
uint64_t __pad;
uint32_t action;
} TrackerRequest;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ConnectRequest_t {
// protocol magic: 0x41727101980
uint64_t protocol_id;
uint32_t action;
uint32_t transaction_id;
} ConnectRequest;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ConnectResponse_t {
uint32_t action;
uint32_t transaction_id;
uint64_t connection_id;
} ConnectResponse;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct AnnounceRequest_t {
uint64_t connection_id;
uint32_t action;
uint32_t transaction_id;
InfoHash info_hash;
unsigned char peer_id[20];
uint64_t downloaded;
uint64_t left;
uint64_t uploaded;
uint32_t event;
// default = 0 (or IPv6 alway is zero)
uint32_t address;
uint32_t key;
int32_t num_want;
uint16_t port;
} AnnounceRequest;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct AnnounceResponse_t {
uint32_t action;
uint32_t transaction_id;
uint32_t interval;
uint32_t leechers;
uint32_t seeders;
} AnnounceResponse;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct AnnounceResponseIPv4Address_t {
uint32_t address;
uint16_t port;
} AnnounceResponseIPv4Address;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct AnnounceResponseIPv6Address_t {
unsigned char address[16];
uint16_t port;
} AnnounceResponseIPv6Address;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ScrapeRequest_t {
uint64_t connection_id;
// action = 2
uint32_t action;
uint32_t transaction_id;
} ScrapeRequest;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ScrapeRequestInfoHash_t {
InfoHash info_hash;
} ScrapeRequestInfoHash;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ScrapeResponse_t {
// action = 2
uint32_t action;
uint32_t transaction_id;
} ScrapeResponse;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ScrapeResponseInfo_t {
uint32_t seeders;
uint32_t completed;
uint32_t leechers;
} ScrapeResponseInfo;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct ErrorResponse_t {
// action = 3
uint32_t action;
uint32_t transaction_id;
char message[256];
} ErrorResponse;
#pragma pack(pop)
_Static_assert(20 == sizeof(InfoHash), "sizeof(union InfoHash_u) != 20");
_Static_assert(16 == sizeof(ConnectRequest), "sizeof(struct ConnectRequest_t) != 16");
_Static_assert(16 == sizeof(ConnectResponse), "sizeof(struct ConnectResponse_t) != 16");
_Static_assert(98 == sizeof(AnnounceRequest), "sizeof(struct AnnounceRequest_t) != 98");
_Static_assert(20 == sizeof(AnnounceResponse), "sizeof(struct AnnounceResponse_t) != 20");
_Static_assert(6 == sizeof(AnnounceResponseIPv4Address), "sizeof(struct AnnounceResponseIPv4Address_t) != 6");
_Static_assert(18 == sizeof(AnnounceResponseIPv6Address), "sizeof(struct AnnounceResponseIPv6Address_t) != 18");
_Static_assert(16 == sizeof(ScrapeRequest), "sizeof(struct ScrapeRequest_t) != 16");
_Static_assert(20 == sizeof(ScrapeRequestInfoHash), "sizeof(struct ScrapeRequestInfoHash_t) != 20");
_Static_assert(8 == sizeof(ScrapeResponse), "sizeof(struct ScrapeResponse_t) != 8");
_Static_assert(12 == sizeof(ScrapeResponseInfo), "sizeof(struct ScrapeResponseInfo_t) != 12");
_Static_assert(264 == sizeof(ErrorResponse), "sizeof(struct ErrorResponse_t) != 264");
//////////////////////////////////////////////////////////
//// Struct - Tables
//////////////////////////////////////////////////////////
typedef struct PeerConnV4_t
{
uint16_t port;
uint32_t address;
} PeerConnV4;
struct ResourcePeerIndexTable_t;
typedef struct Resource_t {
int numOfPeers;
InfoHash infoHash;
uint32_t peerIdx[RESOURCE_PEERS_TABLE_SIZE];
} ResourceTable;
typedef struct TrackerContextConfig_t {
uint32_t announceInterval;
uint32_t numOfSupportPeers;
uint32_t numOfSupportTorrents;
} TrackerContextConfig;
typedef struct TrackerContext_t {
TrackerContextConfig* cfg;
uint32_t numOfOnlinePeers;
PeerConnV4* peerPool;
ResourceTable* resourceTable;
unsigned char* AvailableBitFieldsTables;
} TrackerContext;
//////////////////////////////////////////////////////////
//// Function
//////////////////////////////////////////////////////////
TrackerContext* CreateTrackerServer(TrackerContextConfig* cfg);
void DeinitTrackerServer(TrackerContext* ctx);
void HandleClientRequestV4(TrackerContext* ctx, uint32_t address, char* rxbuf, int32_t rxlen, char* txbuf, int32_t* txlen);