Changes to original code

This commit is contained in:
Nikolaj
2021-11-04 12:08:55 +01:00
parent 74ec5b4ad1
commit 9856de01e4
4 changed files with 55 additions and 13 deletions

View File

@ -3,9 +3,14 @@
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <endian.h>
#include <string.h>
#ifdef __APPLE__
#include "./endian.h"
#else
#include <endian.h>
#endif
#include "./cascade.h"
#include "./sha256.h"
@ -17,7 +22,7 @@ char my_port[PORT_LEN];
struct csc_file *casc_file;
csc_block_t** queue;
csc_peer_t* peers;
void free_resources()
{
free(queue);
@ -27,7 +32,6 @@ void free_resources()
unsigned char* get_file_sha(const char* sourcefile, csc_file_t* res, char* hash, int size)
{
int n;
int casc_file_size;
FILE* fp = fopen(sourcefile, "rb");
@ -87,7 +91,7 @@ void download_only_peer(char *cascade_file)
int peercount = 0;
while (peercount == 0)
{
peercount = get_peers_list(&peers, hash_buf, tracker_ip, tracker_port);
peercount = get_peers_list(&peers, hash_buf);
if (peercount == 0)
{
printf("No peers were found. Will try again in %d seconds\n", PEER_REQUEST_DELAY);
@ -202,6 +206,10 @@ csc_file_t* csc_parse_file(const char* sourcefile, const char* destination)
csc_file_t* casc_file_data = (csc_file_t*)malloc(sizeof(csc_file_t));
casc_file_data->targetsize = be64toh(*((unsigned long long*)&header[16]));
casc_file_data->blocksize = be64toh(*((unsigned long long*)&header[24]));
/*
TODO Parse the cascade file and store the data in an appropriate data structure
@ -300,7 +308,7 @@ void get_block(csc_block_t* block, csc_peer_t peer, unsigned char* hash, char* o
{
printf("Failed to open destination: %s\n", output_file);
Close(peer_socket);
return NULL;
return;
}
/*
@ -312,7 +320,7 @@ void get_block(csc_block_t* block, csc_peer_t peer, unsigned char* hash, char* o
fclose(fp);
}
int get_peers_list(csc_peer_t** peers, unsigned char* hash, char* tracker_ip, char* tracker_port, char* my_ip, char* my_port)
int get_peers_list(csc_peer_t** peers, unsigned char* hash)
{
rio_t rio;
char rio_buf[MAX_LINE];
@ -358,7 +366,7 @@ int get_peers_list(csc_peer_t** peers, unsigned char* hash, char* tracker_ip, ch
return NULL;
}
memset(error_buf, 0, msglen + 1);
memcpy(reply_header, error_buf, msglen);
memcpy(error_buf, &rio_buf[REPLY_HEADER_SIZE], msglen); // Fixed by Rune
printf("Tracker gave error: %d - %s\n", reply_header[0], error_buf);
free(error_buf);
Close(tracker_socket);