-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata.c
More file actions
501 lines (413 loc) · 16.5 KB
/
data.c
File metadata and controls
501 lines (413 loc) · 16.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include <time.h>
#include <memory.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include "data.h"
#include "alloc.h"
#include "socket_udp_structure.h"
#include "data_structure.h"
#include "socket.h"
#define DATA_SEPARATE_TCP_AND_UDP_CLIENTS 0
/*
* Приблизительно максимальный размер сообщения анонса и скрапа
* Максимально допустимые размер UDP данных 65 527 байт
*/
#define DATA_MAX_PACKET_SIZE 64000
struct renderAnnouncePeersCallbackL {
struct block *block;
unsigned int leftPeers;
struct query *query;
};
struct renderScrapeTorrentsCallbackL {
struct block *block;
struct query *query;
};
void int2ip(char *dest, struct in6_addr *source, unsigned char ipVersion);
void changeTorrentStatsL(struct item *torrent, unsigned char oldEvent, unsigned char newEvent);
struct item *setTorrentL(struct list *torrentList, unsigned char *hash) {
struct item *torrent = setHash(torrentList, hash);
if (torrent->data == NULL) {
torrent->data = c_calloc(1, sizeof(struct torrentDataL));
((struct torrentDataL *) torrent->data)->peerList =
initList(NULL, 0, LIST_STARTING_NEST, URI_PARAM_VALUE_LENGTH, LIST_SEMAPHORE_DISABLE, BIG_ENDIAN);
}
return torrent;
}
/**
* Рендер отдельного пира для анонса
* @param list
* @param peer
* @param args
* @return
*/
unsigned char renderAnnouncePeersCallback(struct list *list, struct item *peer, void *args) {
if (list == NULL) {
// error: unused parameter 'list' [-Werror,-Wunused-parameter]
}
struct renderAnnouncePeersCallbackL *mapPeersL = args;
struct query *query = mapPeersL->query;
struct block *peerBlock = mapPeersL->block;
struct peerDataL *peerDataL = peer->data;
if (peerBlock->size >= DATA_MAX_PACKET_SIZE) {
return LIST_BREAK_RETURN;
}
if (DATA_SEPARATE_TCP_AND_UDP_CLIENTS) {
// Ищу TCP, но пир не TCP
if (query->protocol == URI_QUERY_PROTOCOL_TCP
&& (peerDataL->protocol & DATA_STRUCTURE_PEER_PROTOCOL_TCP_BIT) == 0) {
return LIST_CONTINUE_RETURN;
}
// Ищу UDP, но пир не UDP
if (query->protocol == URI_QUERY_PROTOCOL_UDP
&& (peerDataL->protocol & DATA_STRUCTURE_PEER_PROTOCOL_UDP_BIT) == 0) {
return LIST_CONTINUE_RETURN;
}
}
// Версии протоколов разные (ipv4/ipv6)
if ((query->ipVersion & peerDataL->ipVersion) == 0) {
return LIST_CONTINUE_RETURN;
}
char ip[INET6_ADDRSTRLEN] = {0};
if (query->compact || query->protocol == URI_QUERY_PROTOCOL_UDP) {
// todo сделать правильный разбор ip4/6
if (query->ipVersion & SOCKET_VERSION_IPV4_BIT) {
unsigned char *ip6 = (unsigned char *) &peerDataL->ip4;
unsigned char *ip4 = ip6 + 12;
addStringBlock(peerBlock, ip4, sizeof(int));
} else
addStringBlock(peerBlock, &peerDataL->ip6, sizeof(struct in6_addr));
addStringBlock(peerBlock, &peerDataL->port, sizeof(short));
} else if (query->no_peer_id) {
// todo сделать правильный разбор ip4/6
if (query->ipVersion & SOCKET_VERSION_IPV4_BIT)
int2ip(ip, &peerDataL->ip4, query->ipVersion);
else
int2ip(ip, &peerDataL->ip6, query->ipVersion);
addFormatStringBlock(peerBlock, 500,
"d"
"4:port" "i%de"
"2:ip" "%lu:%s"
"e",
htons(peerDataL->port),
strlen(ip),
ip
);
} else {
addFormatStringBlock(peerBlock, 500,
"d"
"4:port" "i%de"
"7:peer id" "%d:",
htons(peerDataL->port),
URI_PARAM_VALUE_LENGTH
);
addStringBlock(peerBlock, peer->hash, URI_PARAM_VALUE_LENGTH);
// todo сделать правильный разбор ip4/6
if (query->ipVersion & SOCKET_VERSION_IPV4_BIT)
int2ip(ip, &peerDataL->ip4, query->ipVersion);
else
int2ip(ip, &peerDataL->ip6, query->ipVersion);
addFormatStringBlock(peerBlock, 500,
"2:ip" "%lu:%s"
"e",
strlen(ip),
ip
);
}
return --mapPeersL->leftPeers <= 0;
}
/**
* Рендер пиров торрента для анонса
* @param block
* @param peerList
* @param query
*/
void renderAnnouncePeersL(struct block *block, struct list *peerList, struct query *query) {
struct renderAnnouncePeersCallbackL mapPeersL;
mapPeersL.block = block;
mapPeersL.leftPeers = query->numwant; // Изменяемое значение
mapPeersL.query = query;
mapList(peerList, &mapPeersL, &renderAnnouncePeersCallback);
}
/**
* Рендер заголовка анонса
* @param block
* @param torrent
* @param query
* @param interval
*/
void renderAnnounceTorrentL(struct block *block, struct block *announceBlock, struct item *torrent, struct query *query,
struct interval *interval) {
struct torrentDataL torrentDataL = {};
if (torrent != NULL)
memcpy(&torrentDataL, torrent->data, sizeof(struct torrentDataL));
struct list *peerList = torrentDataL.peerList;
announceBlock = resetBlock(announceBlock);
renderAnnouncePeersL(announceBlock, peerList, query);
char *peersByVersion = (query->ipVersion & SOCKET_VERSION_IPV4_BIT) ? "5:peers" : "6:peers6";
if (query->protocol == URI_QUERY_PROTOCOL_UDP) {}
else if (query->compact) {
addFormatStringBlock(block, 500,
"d"
"8:complete" "i%de"
"10:incomplete" "i%de"
"10:downloaded" "i%de"
"8:interval" "i%de"
"%s"
"%d:",
torrentDataL.complete,
torrentDataL.incomplete,
torrentDataL.downloaded,
interval->interval,
peersByVersion,
announceBlock->size
);
} else {
addFormatStringBlock(block, 500,
"d"
"8:complete" "i%de"
"10:incomplete" "i%de"
"10:downloaded" "i%de"
"8:interval" "i%de"
"%s"
"l",
torrentDataL.complete,
torrentDataL.incomplete,
torrentDataL.downloaded,
interval->interval,
peersByVersion
);
}
addStringBlock(block, announceBlock->data, announceBlock->size);
if (query->protocol == URI_QUERY_PROTOCOL_TCP) {
if (!query->compact) {
addStringBlock(block, "e", sizeof(char));
}
addStringBlock(block, "e", sizeof(char));
}
}
/**
* Рендер анонса
* @param block
* @param torrent
* @param query
* @param interval
*/
void renderAnnouncePublic(struct block *block, struct block *announceBlock, struct item *torrent, struct query *query,
struct interval *interval) {
struct torrentDataL torrentDataL = {};
if (torrent != NULL)
memcpy(&torrentDataL, torrent->data, sizeof(struct torrentDataL));
if (query->protocol == URI_QUERY_PROTOCOL_UDP) {
struct announceHeadResponse announceHeadResponse = {};
announceHeadResponse.action = ntohl(SOCKET_UDP_STRUCTURE_ACTION_ANNOUNCE);
announceHeadResponse.transaction_id = query->transaction_id;
announceHeadResponse.interval = ntohl(interval->interval);
announceHeadResponse.leechers = ntohl(torrentDataL.incomplete);
announceHeadResponse.seeders = ntohl(torrentDataL.complete);
addStringBlock(block, &announceHeadResponse, sizeof(struct announceHeadResponse));
}
renderAnnounceTorrentL(block, announceBlock, torrent, query, interval);
}
/**
* Рендер статитики пиров одного торрента для скрапа
* @param block
* @param torrent
* @param hash
* @param protocol
*/
void renderScrapeTorrentL(struct block *block, struct item *torrent, unsigned char *hash, _Bool protocol) {
struct torrentDataL torrentDataL = {};
if (torrent != NULL)
memcpy(&torrentDataL, torrent->data, sizeof(struct torrentDataL));
if (protocol == URI_QUERY_PROTOCOL_UDP) {
struct scrapeTorrentResponse scrapeTorrentResponse = {};
scrapeTorrentResponse.seeders = htonl(torrentDataL.complete);
scrapeTorrentResponse.completed = htonl(torrentDataL.downloaded);
scrapeTorrentResponse.leechers = htonl(torrentDataL.incomplete);
addStringBlock(block, &scrapeTorrentResponse, sizeof(struct scrapeTorrentResponse));
} else {
addStringBlock(block, "20:", 3);
addStringBlock(block, hash, URI_PARAM_VALUE_LENGTH);
addFormatStringBlock(block, 1000, "d"
"8:completei%de"
"10:incompletei%de"
"10:downloadedi%de"
"e",
torrentDataL.complete,
torrentDataL.incomplete,
torrentDataL.downloaded);
}
}
/**
* Перебор всех торрентов для скрапа
* @param list
* @param torrent
* @param args
* @return
*/
unsigned char renderScrapeTorrentsCallback(struct list *list, struct item *torrent, void *args) {
if (list == NULL) {
// error: unused parameter 'list' [-Werror,-Wunused-parameter]
}
struct renderScrapeTorrentsCallbackL *renderScrapeTorrentsCallbackL = args;
renderScrapeTorrentL(renderScrapeTorrentsCallbackL->block, torrent, torrent->hash,
renderScrapeTorrentsCallbackL->query->protocol);
return LIST_CONTINUE_RETURN;
}
/**
* Рендер скрапа
* @param block
* @param torrentList
* @param hashes
* @param query
*/
void
renderScrapeTorrentsPublic(struct block *block, struct block *scrapeBlock, struct list *torrentList,
struct block *hashes, struct query *query) {
scrapeBlock = resetBlock(scrapeBlock);
_Bool protocol = query->protocol;
if (protocol == URI_QUERY_PROTOCOL_UDP) {
struct scrapeHeadResponse scrapeHeadResponse = {};
scrapeHeadResponse.action = htonl(SOCKET_UDP_STRUCTURE_ACTION_SCRAPE);
scrapeHeadResponse.transaction_id = query->transaction_id;
addStringBlock(block, &scrapeHeadResponse, sizeof(struct scrapeHeadResponse));
} else {
addStringBlock(block, "d"
"5:files"
"d", 9);
}
if (hashes->size == 0 && DATA_FULL_SCRAPE_ENABLE) {
struct renderScrapeTorrentsCallbackL renderScrapeTorrentsCallbackL = {};
renderScrapeTorrentsCallbackL.query = query;
renderScrapeTorrentsCallbackL.block = scrapeBlock;
mapList(torrentList, &renderScrapeTorrentsCallbackL, &renderScrapeTorrentsCallback);
} else {
unsigned char hash[URI_PARAM_VALUE_LENGTH];
for (unsigned int index = 0; index < hashes->size / URI_PARAM_VALUE_LENGTH; ++index) {
memcpy(&hash, &hashes->data[index * URI_PARAM_VALUE_LENGTH], URI_PARAM_VALUE_LENGTH);
struct list *leaf = getLeaf(torrentList, hash);
waitSemaphoreLeaf(leaf);
struct item *torrent = getHash(torrentList, hash);
renderScrapeTorrentL(scrapeBlock, torrent, hash, protocol);
postSemaphoreLeaf(leaf);
if (scrapeBlock->size >= DATA_MAX_PACKET_SIZE) {
break;
}
}
}
addStringBlock(block, scrapeBlock->data, scrapeBlock->size);
if (protocol == URI_QUERY_PROTOCOL_TCP) {
addStringBlock(block, "e"
"e", 2);
}
}
/**
* Удаляет пира из торрента
* @param torrentList
* @param query
* @return
*/
struct item *deletePeerPublic(struct list *torrentList, struct query *query) {
struct item *torrent = getHash(torrentList, query->info_hash);
if (torrent != NULL) {
struct list *peerList = ((struct torrentDataL *) torrent->data)->peerList;
struct item *peer = getHash(peerList, query->peer_id);
if (peer != NULL) {
struct peerDataL *peerDataL = peer->data;
// Удаление из статистики
changeTorrentStatsL(torrent, peerDataL->event, URI_EVENT_ID_NONE);
deleteItem(peer);
}
}
return torrent;
}
/**
* Обновляет пира в торренте
* @param torrentList
* @param query
* @return
*/
struct item *setPeerPublic(struct list *torrentList, struct query *query, unsigned char protocol, struct stats *stats) {
struct item *torrent = setTorrentL(torrentList, query->info_hash);
struct torrentDataL *torrentDataL = torrent->data;
struct list *peerList = torrentDataL->peerList;
if (torrentDataL->complete + torrentDataL->incomplete > DATA_STRUCTURE_PEERS_FOR_LEVEL_0_LIMIT
&& peerList->level == 0) {
peerList = reInitList(peerList, 1);
torrentDataL->peerList = peerList;
}
struct item *peer = setHash(peerList, query->peer_id);
if (peer->data == NULL) {
peer->data = c_calloc(1, sizeof(struct peerDataL));
// Новый пир
changeTorrentStatsL(torrent, URI_EVENT_ID_NONE, query->event);
} else {
// Уже имеющийся пир
changeTorrentStatsL(torrent, ((struct peerDataL *) peer->data)->event, query->event);
}
struct peerDataL *peerData = (struct peerDataL *) peer->data;
peerData->port = query->port;
if (query->ipVersion & SOCKET_VERSION_IPV4_BIT)
peerData->ip4 = query->ip;
else
peerData->ip6 = query->ip;
time_t now = time(NULL);
unsigned int delay = 0;
if (peerData->updateTime != 0)
delay = now - peerData->updateTime;
updatePeerStat(stats, delay);
peerData->updateTime = now;
peerData->event = query->event;
peerData->protocol = peerData->protocol | protocol;
peerData->ipVersion = peerData->ipVersion | query->ipVersion;
return torrent;
}
/**
* Обновляет статистику пировов торрента
* @param torrent
* @param oldEvent
* @param newEvent
*/
void changeTorrentStatsL(struct item *torrent, unsigned char oldEvent, unsigned char newEvent) {
struct torrentDataL *torrentDataL = torrent->data;
if (oldEvent == URI_EVENT_ID_NONE) {
// Новый пир
if (newEvent == URI_EVENT_ID_COMPLETED) {
torrentDataL->downloaded++;
torrentDataL->complete++;
} else {
torrentDataL->incomplete++;
}
} else if (newEvent == URI_EVENT_ID_NONE) {
// Удаляем пир (пир есть в базе, он был добавлен, он действительно удаляется)
if (oldEvent == URI_EVENT_ID_COMPLETED) {
torrentDataL->complete--;
} else {
torrentDataL->incomplete--;
}
} else if (oldEvent != newEvent && newEvent != URI_EVENT_ID_CONTINUE) {
// Пир меняет статус
if (oldEvent == URI_EVENT_ID_COMPLETED) {
torrentDataL->complete--;
torrentDataL->incomplete++;
} else if (newEvent == URI_EVENT_ID_COMPLETED) {
torrentDataL->downloaded++;
torrentDataL->complete++;
torrentDataL->incomplete--;
}
}
}
// todo сделать правильный разбор ip4/6
void int2ip(char *dest, struct in6_addr *source, unsigned char ipVersion) {
unsigned char *src6 = (unsigned char *) source;
unsigned char *src4 = src6 + 12;
memset(dest, 66, INET6_ADDRSTRLEN);
if (ipVersion & SOCKET_VERSION_IPV4_BIT) {
if (inet_ntop(AF_INET, src4, dest, INET_ADDRSTRLEN)) {
}
} else {
if (inet_ntop(AF_INET6, source, dest, INET6_ADDRSTRLEN)) {
}
}
}