-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece_manager.c
596 lines (480 loc) · 19.9 KB
/
piece_manager.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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "piece_manager.h"
uint8_t * myBitfield;
int maxNumPiece;
Torrent * torrentCopy;
uint32_t uploadedSubPiece = 0;
Torrent * piece_manager_get_torrent(){
return torrentCopy;
}
uint32_t num_pieces_downloaded() {
uint32_t n = 0;
for (int i = 0; maxNumPiece > i; i++) {
if (have_piece(myBitfield, i)) {
n++;
}
}
return n;
};
uint32_t num_piece_upload(){
return uploadedSubPiece;
}
void piece_manager_startup(Torrent * torrent) {
// Set initial for list
torrentCopy = torrent;
// Set initial for list
init_download_pipe();
init_upload_pipe();
init_requested_piece();
// Create folder for pieces if don't exist
char cwd1[PATH_MAX];
if (getcwd(cwd1, sizeof(cwd1)) != NULL) {
// DEBUG_PRINTF("1 Current working dir: %s\n", cwd1);
}
/*
for(int i = 0; g_torrent->num_pieces > i; i++) {
uint8_t* piece_hash = g_torrent->piece_hashes[i];
uint8_t* piece_hash_str = sha1_to_hexstr(piece_hash);
printf("%d -> %s\n", i, piece_hash_str);
}
*/
// exit(1);
mkdir(".torrent_data", 0777);
chdir(".torrent_data");
mkdir(torrent->hash_str, 0777);
chdir(torrent->hash_str);
mkdir("temp", 0777);
chdir("../../");
char cwd2[PATH_MAX];
if (getcwd(cwd2, sizeof(cwd2)) != NULL) {
// DEBUG_PRINTF("2 Current working dir: %s\n", cwd2);
}
maxNumPiece = torrent->num_pieces;
myBitfield = malloc((int) ceil((double) maxNumPiece / 8));
memset(myBitfield, 0, (int) ceil((double) maxNumPiece / 8));
// Bitfield - 1 represent have, 0 represent don't have
// Part of the Path to the folder that hold the pieces download so far
char folderPath[100];
memset(folderPath, 0, sizeof(char) * 100);
folderPath[0] = '\0';
strcat(folderPath, "./.torrent_data/");
strcat(folderPath, torrent->hash_str);
// strcat(folderPath, "/");
int pieces_loaded = 0;
DIR *folder;
struct dirent *file;
if((folder = opendir(folderPath)) != NULL){
while((file = readdir(folder)) != NULL){
if(strlen(file->d_name) > 5){
char* pieceName;
if (strlen(file->d_name) != 40) {
continue;
}
pieceName = file->d_name;
// Convert pieceHash to byte
uint8_t pieceHash[20];
hexstr_to_sha1(pieceHash, pieceName);
// Get piece index from hash and set bitfield
int pieceIndex = torrent_hash_to_piece_index(pieceHash);
// printf("Hash to Index: %s --> %d\n", pieceName, pieceIndex);
if (pieceIndex < 0) {
printf("Error: corrupted piece found; unknown piece hash.\n");
continue;
}
// Validate the hash
char piece_path[1024];
piece_path[0] = 0;
get_piece_filename(piece_path, pieceIndex, 0);
uint8_t* piece_hash = sha1_file(piece_path);
if (piece_hash == NULL) {
continue;
}
char* piece_hexstr = sha1_to_hexstr(piece_hash);
uint8_t* real_piece_hash = g_torrent->piece_hashes[pieceIndex];
char* real_piece_hexstr = sha1_to_hexstr(real_piece_hash);
if (strcmp(piece_hexstr, real_piece_hexstr) != 0) {
printf("Error: corrupted piece=%d found; hash doesn't match. deleting.\n",
pieceIndex);
printf("\texpected: %s\n\treceived: %s\n", real_piece_hexstr, piece_hexstr);
// remove(piece_path);
continue;
}
// Set the bitfield
if(pieceIndex >= 0){
set_have_piece(myBitfield, pieceIndex);
pieces_loaded++;
}
}
}
closedir(folder);
}
printf("Loaded %d/%d pieces from storage\n",
(int) pieces_loaded, (int) g_torrent->num_pieces);
}
int piece_manager_am_interested(struct Peer * peer){
uint8_t * peerBitfield = peer->bitfield;
int result = 0;
for(int i = 0; i < maxNumPiece; i++){
if(!have_piece(myBitfield, i) && have_piece(peerBitfield, i)){
result = 1;
break;
}
}
return result;
}
void piece_manager_cancel_request_for_peer(struct Peer * peer){
remove_request_from_peer(peer->socket);
}
uint8_t * piece_manager_get_my_bitfield() {
return myBitfield;
}
int piece_manager_get_my_bitfield_size() {
return (int) ceil((double) maxNumPiece / 8);
}
/*
This function will initiate an upload/download by creating
a new thread and executing "begin_upload_download" - it will
also create an I/O pipe which will be the primary method of
communication between the subordiante threead & main thread.
*/
void piece_manager_begin_upload_download(
int is_upload,
struct Peer* peer,
uint32_t pieceIndex,
int begin,
int len
) {
// Create fd's for I/O with the subordinate (0=read, 1=write)
int fd[2]; // 0=read, 1=write
pipe(fd);
if(is_upload)
uploadedSubPiece += len;
DEBUG_PRINTF("[Piece Manager] begin up/dl. begin=%d, len=%d, piece=%d\n",
begin, len, pieceIndex);
// Malloc args for the upload/download manager
UploadDownloadManagerArgs* args = malloc(sizeof(UploadDownloadManagerArgs));
args->is_upload = is_upload;
args->write_fd = fd[1];
args->peer = peer;
args->pieceIndex = pieceIndex;
args->begin = begin;
args->len = len;
// Store the descriptor
record_upload_download_pipe(is_upload, fd[0], pieceIndex, peer->socket);
// Create a thread for the process
pthread_t tid;
pthread_create(&tid, NULL, begin_upload_download, (void*) args);
};
void piece_manager_create_download_manager(
struct Peer* peer, uint32_t pieceIndex, int pieceSize, int begin
) {
piece_manager_begin_upload_download(0, peer, pieceIndex, begin, pieceSize);
};
void piece_manager_create_upload_manager(
struct Peer* peer, uint32_t pieceIndex, int pieceSize, int begin
) {
piece_manager_begin_upload_download(1, peer, pieceIndex, begin, pieceSize);
};
// Current code can request multiple piece to same peer if that piece is among the rarest.
void piece_manager_initiate_download() {
struct Peer * peerList = peer_manager_get_root_peer();
bool anyInterested = false;
while (peerList != NULL)
{
anyInterested = anyInterested || piece_manager_am_interested(peerList);
peerList = peerList->next;
}
// DEBUG_PRINTF("Is there any peer that I am still interested in? %d\n", anyInterested);
if (DEBUG_CURRENTLY_DOWNLOADING && g_debug == 1) {
DEBUG_PRINTF("[Debug Mode] Skipping begin now download, download in progress\n");
return;
}
struct Peer * p = peer_manager_get_root_peer();
int numOpen = 0;
while(p != NULL){
if(p->peer_choking == 0 && p->curr_up != 1){
numOpen++;
}
p = p->next;
}
struct OpenPeer * listOpenPeer = malloc(numOpen * sizeof(struct OpenPeer));
p = peer_manager_get_root_peer();
int index = 0;
while(p != NULL){
if(p->peer_choking == 0){
listOpenPeer[index].peer = p;
index++;
}
p = p->next;
}
struct Peer * smallest = NULL; // The peer that have the minPiece that client will send request to
int minOccur = INT_MAX; // The number of peer that have the minPiece
int minPiece = -1; // The current rarest piece
// DEBUG_PRINTF("[Piece Manager] Downloading piece: %d/%d!!\n", maxNumPiece);
for(int i = 0; i < maxNumPiece; i++){
// Check that client don't have piece and had not send a request for the piece
/*
if(i == 85) {
DEBUG_PRINTF("Checking 86th piece\n");
DEBUG_PRINTF("Do I not have the piece %d\n Am I currently requesting the piece? %d\n Peer Sock %d\n ",
!have_piece(myBitfield, i),
!currently_requesting_piece(i),
get_peer_socket_from_piece(i));
}
*/
if(!have_piece(myBitfield, i) && !currently_requesting_piece(i)){
// printf("checking for piece %d\n", i);
int chance = 6;
int currentOccur = 0;
int pos;
//struct Peer * currentPeer = peer_manager_get_root_peer();
struct Peer * currentSmallest = NULL;
for(pos = 0; pos < numOpen; pos++){
// Client is interested and peer is not choking and
// have current look at piece that client don't have
if(i == 85){
/*
DEBUG_PRINTF("Had not requesting from this peer %d \n not downloading from this peer %d \n peer is not choking %d \n peer have this piece %d\n",
!currently_requesting_piece_from(listOpenPeer[pos].peer->socket),
!(listOpenPeer[pos].peer)->curr_dl,
(listOpenPeer[pos].peer)->peer_choking == 0,
have_piece((listOpenPeer[pos].peer)->bitfield, i));
*/
}
if(
!currently_requesting_piece_from(listOpenPeer[pos].peer->socket) &&
!(listOpenPeer[pos].peer)->curr_dl &&
(listOpenPeer[pos].peer)->peer_choking == 0 &&
have_piece((listOpenPeer[pos].peer)->bitfield, i)
) {
currentOccur++;
// listOpenPeer[pos].peer is sometimes null
if(chance >= 5){
currentSmallest = listOpenPeer[pos].peer;
}
chance = rand() % 10;
}
}
if(minOccur > currentOccur && currentOccur != 0){
minOccur = currentOccur;
smallest = currentSmallest;
minPiece = i;
}
}
}
if(minPiece != -1) {
DEBUG_PRINTF("[Piece Manager] Beginning download on piece index : %d\n", minPiece);
if (smallest == NULL) {
DEBUG_PRINTF("-- error: piece manager selected NULL peer!\n");
}
peer_manager_begin_download(smallest, minPiece);
// Track piece that have send request for
add_requested_piece(smallest->socket, minPiece);
} else {
DEBUG_PRINTF("[Piece Manager] could not identify piece to download!\n");
}
struct Peer* ptr = peer_manager_get_root_peer();
uint16_t num_unchoked = 0;
while (ptr != NULL) {
if (ptr->peer_choking == 0) {
num_unchoked++;
}
ptr = ptr->next;
}
DEBUG_PRINTF("- %d peers UNCHOKING us\n", num_unchoked);
free(listOpenPeer);
}
bool piece_manager_cancel_request(uint32_t pieceIndex){
if(!is_currently_downloading_piece(pieceIndex)){
remove_requested_piece(pieceIndex);
return true;
}
return false;
}
void piece_manager_periodic() {
piece_manager_initiate_download();
struct timeval waitingTime;
fd_set downloadPipeSet;
fd_set uploadPipeSet;
struct pairList * downloadPipeList = get_download_pipe();
struct pairList * uploadPipeList = get_upload_pipe();
// Process download pipe
struct pairList * currentElem = downloadPipeList->next;
int maxPipe = 0;
FD_ZERO(&downloadPipeSet);
while(currentElem != downloadPipeList){
FD_SET(currentElem->sock, &downloadPipeSet);
maxPipe = maxPipe > currentElem->sock ? maxPipe : currentElem->sock;
currentElem = currentElem->next;
}
waitingTime.tv_sec = 0;
waitingTime.tv_usec = 100;
int activity = select(maxPipe + 1, &downloadPipeSet, NULL, NULL, &waitingTime);
if(activity < 0){
perror("select() fail for download pipe");
exit(EXIT_FAILURE);
}
// DEBUG_PRINTF("[Piece Manager Periodic] Checking to see if any new DL info came in\n");
currentElem = downloadPipeList->next;
while(currentElem != downloadPipeList){
if(FD_ISSET(currentElem->sock, &downloadPipeSet)){
char buffer[1];
read(currentElem->sock, buffer, 1);
// DEBUG_PRINTF("[Piece Manager Periodic] Got code %c\n", buffer[0]);
int currentPipe = currentElem->sock;
uint32_t currentPieceIndex = currentElem->pieceIndex;
int peerSocket = currentElem->peerSock;
struct Peer * currentPeer = NULL;
if(peerSocket != -1){
struct Peer * p = peer_manager_get_root_peer();
while(p != NULL){
if(p->socket == peerSocket){
currentPeer = p;
break;
}
p = p->next;
}
}
if(buffer[0] == 's') {
close(currentElem->sock);
uint32_t totalRemainingByte = torrentCopy->length - (currentElem->pieceIndex * torrentCopy->piece_length);
uint32_t length = torrentCopy->piece_length < totalRemainingByte ? torrentCopy->piece_length : totalRemainingByte;
uint32_t total_subpieces = ceil(length / PIECE_DOWNLOAD_SIZE);
currentElem = currentElem->prev;
remove_upload_download_pipe(0, currentPipe);
// Downloaded the whole piece
if(!(currentPeer->curr_dl_next_subpiece < total_subpieces)) {
remove_requested_piece(currentPieceIndex);
// Get file names
char piece_perm_filename[1024] = {0};
char piece_temp_filename[1024] = {0};
get_piece_filename(piece_perm_filename, currentPieceIndex, 0);
get_piece_filename(piece_temp_filename, currentPieceIndex, 1);
// Validate SHA1 hash
uint8_t* real_piece_hash = g_torrent->piece_hashes[currentPieceIndex];
char* real_piece_hash_hexstr = sha1_to_hexstr(real_piece_hash);
uint8_t* dl_piece_hash = sha1_file(piece_temp_filename);
if (!dl_piece_hash) continue;
char* dl_piece_hexstr = sha1_to_hexstr(dl_piece_hash);
if (strcmp((const char * )dl_piece_hexstr, real_piece_hash_hexstr) != 0) {
printf("[Piece Manager] - Error, dowload for piece=%d corrupted. Hash does not match!\n",
currentPieceIndex);
continue;
} else {
uint32_t num_dl = num_pieces_downloaded();
float pct = ((float) num_dl / (float) g_torrent->num_pieces) * 100.0;
printf("[Piece Manager] Downloaded %d/%d pieces (%d pct complete)\n",
(int) num_dl, (int) g_torrent->num_pieces, (int) pct);
}
// Copy to permanant storage
if (cp(piece_perm_filename, piece_temp_filename) != 0) {
printf("\n[Piece Manager] ERROR!!! Unable to store piece file!!!!\n\n");
} else {
set_have_piece(myBitfield, currentPieceIndex);
}
}
// Delete below when the above is uncommented
// set_have_piece(myBitfield, currentPieceIndex);
// remove_requested_piece(currentPieceIndex);
if(peerSocket != -1){
peer_manager_upload_download_complete(0, currentPeer, currentPieceIndex);
}
if(have_all_piece()){
printf("\nALL PIECES DOWNLOADED!\n");
peer_manager_complete();
file_assembler_begin(torrentCopy);
printf("All files assembled! Terminating.\n\n");
//exit(1);
}
}
else if(buffer[0] == 'r'){
uint8_t rateMsg[8];
int total = 0;
int gotNum = 0;
while(total < 8){
gotNum = read(currentPipe, rateMsg + total, 8 - total);
total += gotNum;
}
uint64_t downloadRate;
memcpy(&downloadRate, rateMsg, 8);
if(peerSocket != -1){
peer_manager_update_download_rate(currentPeer, downloadRate);
}
}
else if(buffer[0] == 'f'){
close(currentElem->sock);
currentElem = currentElem->prev;
remove_upload_download_pipe(0, currentPipe);
remove_requested_piece(currentPieceIndex);
if(peerSocket != -1){
peer_manager_inform_disconnect(currentPeer);
}
}
else{
// Unknow message
}
}
currentElem = currentElem->next;
}
// Process upload pipe
currentElem = uploadPipeList->next;
maxPipe = 0;
FD_ZERO(&uploadPipeSet);
while(currentElem != uploadPipeList){
FD_SET(currentElem->sock, &uploadPipeSet);
maxPipe = maxPipe > currentElem->sock ? maxPipe : currentElem->sock;
currentElem = currentElem->next;
}
waitingTime.tv_sec = 0;
waitingTime.tv_usec = 100;
activity = select(maxPipe + 1, &uploadPipeSet, NULL, NULL, &waitingTime);
if(activity < 0){
perror("select() fail for upload pipe");
exit(EXIT_FAILURE);
}
currentElem = uploadPipeList->next;
while(currentElem != uploadPipeList){
if(FD_ISSET(currentElem->sock, &uploadPipeSet)){
char buffer[100];
read(currentElem->sock, buffer, 19);
int currentPipe = currentElem->sock;
int peerSocket = currentElem->peerSock;
int currentPieceIndex = currentElem->pieceIndex;
struct Peer * currentPeer = NULL;
if(peerSocket != -1){
struct Peer * p = peer_manager_get_root_peer();
while(p != NULL){
if(p->socket == peerSocket){
currentPeer = p;
break;
}
p = p->next;
}
}
if(strcmp(buffer, "s") == 0){
currentElem = currentElem->prev;
remove_upload_download_pipe(1, currentPipe);
peer_manager_upload_download_complete(1, currentPeer, currentPieceIndex);
}
else if(strcmp(buffer, "f") == 0){
currentElem = currentElem->prev;
remove_upload_download_pipe(1, currentPipe);
if(peerSocket != -1){
peer_manager_inform_disconnect(currentPeer);
}
}
else{
// Unknow message
}
}
currentElem = currentElem->next;
}
}
// Check if all pieces had been downloaded
bool have_all_piece(){
bool result = true;
for(int i = 0; i < maxNumPiece; i++){
bool a = have_piece(myBitfield, i);
result = result && a;
}
return result;
}