-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.c
More file actions
226 lines (196 loc) · 6.73 KB
/
Core.c
File metadata and controls
226 lines (196 loc) · 6.73 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "Tracker.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
uint16_t swap16(uint16_t x) {
return ((x & 0xFF00) >> 8) | ((x & 0x00FF) << 8);
}
uint32_t swap32(uint32_t x) {
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0x00FF00FF);
return (x << 16) | (x >> 16);
}
uint64_t swap64(uint64_t num) {
return ((num >> 56) & 0x00000000000000FF) |
((num >> 40) & 0x000000000000FF00) |
((num >> 24) & 0x0000000000FF0000) |
((num >> 8) & 0x00000000FF000000) |
((num << 8) & 0x000000FF00000000) |
((num << 24) & 0x0000FF0000000000) |
((num << 40) & 0x00FF000000000000) |
((num << 56) & 0xFF00000000000000);
}
TrackerContext* CreateTrackerServer(TrackerContextConfig* cfg) {
TrackerContext* ctx = malloc(sizeof(TrackerContext));
if (ctx == NULL) { return NULL; }
ctx->cfg = cfg;
ctx->numOfOnlinePeers = 0;
// malloc peer table
ctx->peerPool = malloc(cfg->numOfSupportPeers * sizeof(PeerConnV4));
// malloc resource table
ctx->resourceTable = malloc(cfg->numOfSupportTorrents * sizeof(ResourceTable));
if (
ctx->peerPool == NULL ||
ctx->resourceTable == NULL ||
ctx->AvailableBitFieldsTables == NULL) {
DeinitTrackerServer(ctx); return NULL;
}
memset(ctx->peerPool, 0, cfg->numOfSupportPeers * sizeof(PeerConnV4));
memset(ctx->resourceTable, 0, cfg->numOfSupportTorrents * sizeof(ResourceTable));
// config network adapter
return ctx;
}
void DeinitTrackerServer(TrackerContext* ctx) {
if (ctx == NULL) { return; }
if (ctx->peerPool) { free(ctx->peerPool); }
if (ctx->resourceTable) { free(ctx->resourceTable); }
if (ctx->cfg) { free(ctx->cfg); }
free(ctx);
}
PeerConnV4* FindEmptyPeerSlot(TrackerContext* ctx) {
uint32_t slotId = 0;
for (; slotId < ctx->cfg->numOfSupportPeers; slotId++) {
PeerConnV4* ptr = (ctx->peerPool + slotId);
if (ptr->address == 0) {
return ptr;
}
}
return NULL;
}
PeerConnV4* FindPeerSlotByConnectionId(TrackerContext* ctx, uint64_t connectionId) {
uint64_t offset = connectionId - (uint64_t)ctx->peerPool;
if (offset % sizeof(PeerConnV4) != 0) { return NULL; }
return (PeerConnV4*)connectionId;
}
uint32_t GetTorrentHashId(TrackerContext* ctx, InfoHash infoHash) {
uint64_t i = infoHash.key ^ infoHash.pos + infoHash.offset;
return (uint32_t)(i % ctx->cfg->numOfSupportTorrents);
}
ResourceTable* FindResourceTableByInfoHash(TrackerContext* ctx, InfoHash infoHash) {
uint32_t i = GetTorrentHashId(ctx, infoHash);
// find loop
do {
ResourceTable* ptr = ctx->resourceTable + i;
if (ptr->numOfPeers == 0) {
return ptr;
}
else {
// Not Empty
if (strncmp(ptr->infoHash.data, infoHash.data, 20) == 0) {
// Hit
return ptr;
}
else {
// Hash Collisions
i++;
}
}
} while (i < ctx->cfg->numOfSupportTorrents);
return NULL;
}
int UpdatePeerInResource(TrackerContext* ctx, ResourceTable* resource, PeerConnV4* peer) {
uint32_t peerIdx = (peer - ctx->peerPool) / sizeof(PeerConnV4);
uint32_t* idx = resource->peerIdx;
int emptyIdx = -1;
uint32_t i = 0;
for (; i < RESOURCE_PEERS_TABLE_SIZE; i++) {
// idx zero is reserve
if (*(idx + i) - 1 == peerIdx) {
break;
}
else if (emptyIdx == -1 && *(idx + i) == 0) {
emptyIdx = i;
}
}
// over limit?
if (i == RESOURCE_PEERS_TABLE_SIZE) {
if (emptyIdx == -1) {
return 1;
}
else {
resource->numOfPeers++;
*(idx + emptyIdx) = peerIdx + 1;
}
}
return 0;
}
int FindPeersInResource(TrackerContext* ctx, ResourceTable* resource, PeerConnV4* peer, AnnounceResponseIPv4Address* addresses, uint8_t* numOfPeers) {
if (*numOfPeers == 0) { *numOfPeers = 200; }
else { *numOfPeers = *numOfPeers > 200 ? 200 : *numOfPeers; }
uint8_t p = 0;
for (int i = 0; i < RESOURCE_PEERS_TABLE_SIZE; i++) {
uint32_t* idx = (resource->peerIdx + i);
if (p == *numOfPeers) {
break;
}
if (*idx != 0) {
PeerConnV4* conn = (ctx->peerPool + (*idx - 1));
AnnounceResponseIPv4Address* addr = (addresses + p++);
addr->address = conn->address;
addr->port = conn->port;
}
}
*numOfPeers = p;
return 0;
}
void HandleClientRequestV4(TrackerContext* ctx, uint32_t address, char* rxbuf, int32_t rxlen, char* txbuf, int32_t* txlen) {
if (rxlen < sizeof(ConnectRequest)) { *txlen = -1; return; }
TrackerRequest* ptr = rxbuf;
if (swap32(ptr->action) == ACTION_CONNECT) {
ConnectRequest* req = ptr;
// Check protocol_id == 0x41727101980
if (swap64(req->protocol_id) != 0x41727101980) { *txlen = -1; return; }
// Find empty peer slot
PeerConnV4* ptr = FindEmptyPeerSlot(ctx);
if (ptr == NULL) { return; }
ptr->address = address;
ptr->port = 0;
// Send connect response
ConnectResponse* resp = txbuf;
resp->action = ACTION_CONNECT;
resp->transaction_id = req->transaction_id;
resp->connection_id = ptr;
*txlen = sizeof(ConnectResponse);
}
else if (swap32(ptr->action) == ACTION_ANNOUNCE) {
AnnounceRequest* req = ptr;
// Find peer by connection_id
PeerConnV4* ptr = FindPeerSlotByConnectionId(ctx, req->connection_id);
if (ptr == NULL || address != ptr->address) { *txlen = -1; return; }
// Find torrent resource
ResourceTable* rt = FindResourceTableByInfoHash(ctx, req->info_hash);
if (rt == NULL) { return; }
if (UpdatePeerInResource(ctx, rt, ptr)) { return; }
// Find seeders or leechers
uint8_t numOfPeers = req->num_want;
if (FindPeersInResource(ctx, rt, ptr, txbuf + sizeof(AnnounceResponse), &numOfPeers)) { return; }
// Send announce response
AnnounceResponse* resp = txbuf;
resp->action = swap32(ACTION_ANNOUNCE);
resp->transaction_id = req->transaction_id;
resp->interval = swap32(ctx->cfg->announceInterval);
resp->leechers = swap32(numOfPeers);
resp->seeders = swap32(0);
*txlen = sizeof(AnnounceResponse) + numOfPeers * sizeof(AnnounceResponseIPv4Address);
}
else if (swap32(ptr->action) == ACTION_SCRAPE) {
ScrapeRequest* req = ptr;
// Find peer by connection_id
PeerConnV4* ptr = FindPeerSlotByConnectionId(ctx, req->connection_id);
if (address != ptr->address) { *txlen = -1; return; }
size_t len = (size_t)((rxlen - sizeof(ScrapeRequest)) / sizeof(ScrapeRequestInfoHash));
ScrapeRequestInfoHash* ih = (char)rxbuf + sizeof(ScrapeRequest);
// Send scrape response
ScrapeResponse* resp = txbuf;
resp->action = swap32(ACTION_SCRAPE);
resp->transaction_id = req->transaction_id;
ScrapeResponseInfo* ihr = (char)rxbuf + sizeof(ScrapeResponse);
// Not support scrape
for (size_t i = 0; i < len; i++) {
ihr->seeders = 0;
ihr->completed = 0;
ihr->leechers = 0;
}
*txlen = sizeof(ScrapeResponse) + sizeof(ScrapeResponseInfo) * len;
}
}