-
Notifications
You must be signed in to change notification settings - Fork 0
/
metainfo.c
346 lines (295 loc) · 9.32 KB
/
metainfo.c
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
/**
* @file metainfo.c
* @brief 操作全局信息的相关 API 实现
*/
#include "metainfo.h"
#include "bparser.h"
#include "butil.h"
#include "peer.h"
#include "connect.h"
#include "util.h"
#include <string.h>
#include <openssl/sha.h>
void
free_metainfo(struct MetaInfo **pmi)
{
struct MetaInfo *mi = *pmi;
*pmi = NULL;
if (mi->trackers) {
free(mi->trackers);
}
if (mi->pieces) {
free(mi->pieces);
}
free(mi);
}
/**
* 观察实际的种子文件, 发现如果有 announce-list, 那么
* announce 往往是其中的第一项. 但是 announce-list 本身
* 不是必须的, 所以在没有 announce-list 是解析 announce
* 否则直接使用 announce-list 忽略 announce.
*/
void
extract_trackers(struct MetaInfo *mi, const struct BNode *ast)
{
// 提取 tracker 列表
const struct BNode *announce_list = query_bcode_by_key(ast, "announce-list");
if (!announce_list) {
// 没有 announce-list, 那么就使用 announce
const struct BNode *announce = query_bcode_by_key(ast, "announce");
mi->nr_trackers = 1;
mi->trackers = calloc(mi->nr_trackers, sizeof(*mi->trackers));
parse_url(announce->s_data,
mi->trackers[0].method,
mi->trackers[0].host,
mi->trackers[0].port,
mi->trackers[0].request);
}
else {
mi->nr_trackers = 0;
for (const struct BNode *iter = announce_list; iter; iter = iter->l_next) {
mi->nr_trackers++;
}
log("%lu trackers", mi->nr_trackers);
mi->trackers = calloc(mi->nr_trackers, sizeof(*mi->trackers));
struct Tracker *tracker = &mi->trackers[0];
for (const struct BNode *iter = announce_list; iter; iter = iter->l_next) {
parse_url(iter->l_item->l_item->s_data,
tracker->method,
tracker->host,
tracker->port,
tracker->request);
tracker++;
}
}
}
void
metainfo_load_file(struct MetaInfo *mi, const struct BNode *ast)
{
const struct BNode *name = query_bcode_by_key(ast, "name");
log("filename: %s", name->s_data);
FILE *fp = fopen(name->s_data, "rb");
// This variable record the downloaded pieces' size.
// We do not use MetaInfo::downloaded as that field is only for data exchanging
// and if MetaInfo::downloaded is 0 as well as MetaInfo::left is, it means this
// client is seeding.
size_t finished = 0;
if (fp != NULL) { // 已有下载文件,检查分片 SHA1
uint8_t *piece = malloc(mi->piece_size); // piece data buffer
uint8_t md[HASH_SIZE]; // sha1 buffer
size_t nr_read;
int piece_index = 0;
while ((nr_read = fread(piece, 1, mi->piece_size, fp)) != 0) { // EOF 退出,容许不足
SHA1(piece, nr_read, md);
printf("piece %d: %lu bytes", piece_index, nr_read);
if (memcmp(md, mi->pieces[piece_index].hash, HASH_SIZE) == 0) { // 分片正确
mi->pieces[piece_index].is_downloaded = 1;
finished += nr_read;
set_bit(mi->bitfield, piece_index);
printf(" ok");
}
printf("\n");
piece_index++;
}
if (finished == mi->file_size) {
log("file has been downloaded");
mi->file = fp;
return;
}
else { // 有不正确的分片,或者文件不完整,重新以可写方式打开。
fclose(fp);
mi->file = fopen(name->s_data, "rb+"); // read, write, no trunc
}
}
else { // 没有下载文件,放心 trunc
mi->file = fopen(name->s_data, "wb");
fwrite("hello", 1, 5, mi->file);
fclose(mi->file);
mi->file = fopen(name->s_data, "rb+");
}
mi->left = mi->file_size - finished;
}
void
extract_pieces(struct MetaInfo *mi, const struct BNode *ast)
{
// 提取分片信息
const struct BNode *length_node = query_bcode_by_key(ast, "length");
if (length_node) {
mi->file_size = (size_t)length_node->i;
}
const struct BNode *piece_length_node = query_bcode_by_key(ast, "piece length");
if (piece_length_node) {
mi->piece_size = (uint32_t)piece_length_node->i;
}
if (mi->file_size && mi->piece_size) {
mi->nr_pieces = (mi->file_size - 1) / mi->piece_size + 1;
mi->bitfield_size = (mi->nr_pieces - 1) / 8 + 1;
mi->sub_size = 0x4000;
mi->sub_count = (mi->piece_size - 1) / mi->sub_size + 1;
mi->bitfield = calloc(mi->bitfield_size, 1);
}
log("filesz %ld, piecesz %d, nr pieces %lu, bitfield len %lu",
mi->file_size, mi->piece_size, mi->nr_pieces, mi->bitfield_size);
log("sub_size %d, sub_count %lu", mi->sub_size, mi->sub_count);
const struct BNode *pieces_node = query_bcode_by_key(ast, "pieces");
if (pieces_node) {
mi->pieces = calloc(mi->nr_pieces, sizeof(*mi->pieces));
const char *hash = pieces_node->s_data;
for (int i = 0; i < mi->nr_pieces; i++) {
memcpy(mi->pieces[i].hash, hash, HASH_SIZE);
hash += HASH_SIZE;
// 最后一个分片可能会造成空间冗余,即子分片不足 sub_count, 但是没有副作用。
mi->pieces[i].substate = calloc(mi->sub_count, sizeof(*mi->pieces[i].substate));
}
}
}
void
add_peer(struct MetaInfo *mi, struct Peer *p)
{
mi->peers = realloc(mi->peers, sizeof(*p) * (mi->nr_peers + 1));
mi->peers[mi->nr_peers] = p;
mi->nr_peers++;
}
/**
* 被删除的指针空闲没有回收, 在未来增加新
* peer 时靠 realloc 重新尾部的冗余空间.
*/
void
del_peer_by_fd(struct MetaInfo *mi, int fd)
{
struct Peer *peer = NULL;
struct Peer **peers = mi->peers;
int i, n = mi->nr_peers;
if (n == 0) {
return;
}
for (i = 0; i < n; i++) {
peer = peers[i];
if (fd == peer->fd) {
break;
}
}
if (peer != NULL) {
peer_free(&peer);
}
if (i < mi->nr_peers -1) {
// 只把要删除的 peer 的后面的 peers 往前拷贝一个。数量计算如下:
// |[i + 1, n - 1]| = (n - 1) - (i + 1) + 1 = n - i - 1;
memmove(peers + i, peers + i + 1, sizeof(*peers) * (n - i - 1));
}
mi->nr_peers--;
}
struct Peer *
get_peer_by_fd(struct MetaInfo *mi, int fd)
{
for (int i = 0; i < mi->nr_peers; i++) {
if (mi->peers[i]->fd == fd) {
return mi->peers[i];
}
}
return NULL;
}
/**
* 参数的 addr 和 port 是网络字节序。
* peer 自己的 addr 在构造时是网络字节序,但是 port 是本机字节序。
*/
struct Peer *
get_peer_by_addr(struct MetaInfo *mi, uint32_t addr, uint16_t port)
{
for (int i = 0; i < mi->nr_peers; i++) {
struct Peer *peer = mi->peers[i];
if (addr == mi->peers[i]->addr && port == mi->peers[i]->port) {
return peer;
}
}
return NULL;
}
int
check_substate(struct MetaInfo *mi, int index)
{
size_t sub_cnt = (index != mi->nr_pieces - 1) ? mi->sub_count :
(((mi->file_size % mi->piece_size) - 1) / mi->sub_size + 1);
int is_finished = 1;
char ch;
for (int i = 0; i < sub_cnt; i++) {
switch (mi->pieces[index].substate[i]) {
case SUB_NA: ch = 'X'; is_finished = 0; break;
case SUB_DOWNLOAD: ch = 'O'; is_finished = 0; break;
case SUB_FINISH: ch = '.'; break;
default: ch = '#'; break;
}
putchar(ch);
}
putchar('\n');
return is_finished;
}
struct Tracker *
get_tracker_by_fd(struct MetaInfo *mi, int sfd)
{
if (sfd == -1) {
return NULL;
}
for (int i = 0; i < mi->nr_trackers; i++) {
if (mi->trackers[i].sfd == sfd) {
return &mi->trackers[i];
}
}
return NULL;
}
struct Tracker *
get_tracker_by_timer(struct MetaInfo *mi, int timerfd)
{
for (int i = 0; i < mi->nr_trackers; i++) {
if (mi->trackers[i].timerfd == timerfd) {
return &mi->trackers[i];
}
}
return NULL;
}
void
add_wait_peer(struct MetaInfo *mi, int fd, uint32_t addr, uint16_t port, int direction)
{
mi->wait_peers = realloc(mi->wait_peers, (mi->nr_wait_peers + 1) * sizeof(struct WaitPeer));
struct WaitPeer *p = &mi->wait_peers[mi->nr_wait_peers];
p->fd = fd;
p->addr = addr;
p->port = port;
p->msg = NULL;
p->wanted = 0;
p->direction = direction;
mi->nr_wait_peers++;
}
int
get_wait_peer_index_by_fd(struct MetaInfo *mi, int fd)
{
for (int i = 0; i < mi->nr_wait_peers; i++) {
if (mi->wait_peers[i].fd == fd) {
return i;
}
}
return -1;
}
int
get_wait_peer_fd(struct MetaInfo *mi, uint32_t addr, uint16_t port)
{
for (int i = 0; i < mi->nr_wait_peers; i++) {
struct WaitPeer *p = &mi->wait_peers[i];
if (p->addr == addr && p->port == port) {
return p->fd;
}
}
return -1;
}
/**
* 如果要删除的元素刚好是最后一个,只需要减少总数量就行了。
*/
void
rm_wait_peer(struct MetaInfo *mi, int index)
{
printf("\n");
if (index < mi->nr_wait_peers - 1) {
memmove(mi->wait_peers + index, mi->wait_peers + index + 1,
sizeof(struct WaitPeer) * (mi->nr_wait_peers - index - 1));
}
mi->nr_wait_peers--;
}