some stuff

This commit is contained in:
Nikolaj
2021-11-04 16:00:22 +01:00
parent 9856de01e4
commit 3237f413db
6 changed files with 124604 additions and 86 deletions

View File

@ -1,4 +1,5 @@
GCC=gcc -O3 -g -Wall -Wextra -pedantic -std=gnu11
#GCC=gcc -O3 -g -Wall -Wextra -pedantic -std=gnu11
GCC=gcc -O3
LD_FLAGS= -lpthread
all: cascade

BIN
A3/src/cascade Executable file

Binary file not shown.

View File

@ -4,6 +4,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <math.h>
#ifdef __APPLE__
#include "./endian.h"
@ -30,7 +31,7 @@ void free_resources()
csc_free_file(casc_file);
}
unsigned char* get_file_sha(const char* sourcefile, csc_file_t* res, char* hash, int size)
unsigned char* get_file_sha(const char* sourcefile, uint8_t hash[32], int size)
{
int casc_file_size;
@ -50,7 +51,7 @@ unsigned char* get_file_sha(const char* sourcefile, csc_file_t* res, char* hash,
fclose(fp);
SHA256_CTX shactx;
unsigned char shabuffer[size];
uint8_t shabuffer[size];
sha256_init(&shactx);
sha256_update(&shactx, buffer, casc_file_size);
sha256_final(&shactx, &shabuffer);
@ -78,15 +79,23 @@ void download_only_peer(char *cascade_file)
printf("Downloading to: %s\n", output_file);
casc_file = csc_parse_file(cascade_file, output_file);
csc_block_t** uncomp = malloc(sizeof(csc_block_t*) * casc_file->blockcount);
int uncomp_count = 0;
for (unsigned long long i = 0;i<casc_file->blockcount;i++) {
if ((&casc_file->blocks[i])->completed == 0) {
uncomp[uncomp_count] = &casc_file->blocks[i];
uncomp_count++;
}
}
/*
TODO Create a list of missing blocks
*/
queue = malloc(sizeof(csc_block_t*) * uncomp_count);
for (int i = 0;i<uncomp_count;i++) {
queue[i] = uncomp[i];
}
free(uncomp);
/*
TODO Compute the hash of the cascade file
HINT: Do not implement hashing from scratch. Use the provided 'get_file_sha' function
*/
uint8_t hash_buf[32];
get_file_sha(cascade_file, hash_buf, 32);
int peercount = 0;
while (peercount == 0)
@ -208,15 +217,45 @@ csc_file_t* csc_parse_file(const char* sourcefile, const char* destination)
casc_file_data->targetsize = be64toh(*((unsigned long long*)&header[16]));
casc_file_data->blocksize = be64toh(*((unsigned long long*)&header[24]));
uint8_t x[32];
for (int i = 0; i < 32; i++) {
x[i] = (uint8_t)(*((unsigned long long*)&header[32+i]));
}
csc_hashdata_t* hash = {x};
casc_file_data->targethash = *hash;
casc_file_data->blockcount = 1 + floor(
(casc_file_data->targetsize - 1.0)/casc_file_data->blocksize
);
casc_file_data->trailblocksize = (casc_file_data->blockcount * casc_file_data->blocksize) - casc_file_data->targetsize;
/*
TODO Parse the cascade file and store the data in an appropriate data structure
csc_block_t* block_list = malloc(
sizeof(csc_block_t) * casc_file_data->blockcount
);
HINT Use the definition of the 'csc_file' struct in cascade.h, as well as the
assignment handout for guidance on what each attribute is and where it is stored
in the files header/body.
*/
casc_file_data->blocks = block_list;
for (unsigned long long b = 0;b < casc_file_data->blockcount; b++) {
csc_block_t* block = &(block_list[b]);
block->index = b;
block->offset = b * casc_file_data->blocksize;
if (b == casc_file_data->blockcount - 1 ) {
block->length = casc_file_data->trailblocksize;
} else {
block->length = casc_file_data->blocksize;
}
block->completed = 0;
uint8_t block_x[32];
if (fread(block_x, 1, 32, fp) != 32) {
printf("Cascade file not readable\n");
fclose(fp);
return NULL;
}
csc_hashdata_t* hash = {block_x};
block->hash = *hash;
}
fclose(fp);
@ -240,7 +279,7 @@ csc_file_t* csc_parse_file(const char* sourcefile, const char* destination)
SHA256_CTX shactx;
for(unsigned long long i = 0; i < casc_file_data->blockcount; i++)
{
char shabuffer[SHA256_HASH_SIZE];
uint8_t shabuffer[SHA256_HASH_SIZE];
unsigned long long size = casc_file_data->blocks[i].length;
if (fread(buffer, size, 1, fp) != 1)
{
@ -251,15 +290,11 @@ csc_file_t* csc_parse_file(const char* sourcefile, const char* destination)
sha256_update(&shactx, buffer, size);
sha256_final(&shactx, &shabuffer);
/*
TODO Compare the hashes taken from the Cascade file with those of the local data
file and keep a record of any missing blocks
HINT The code above takes a hash of each block of data in the local file in turn
and stores it in the 'shabuffer' variable. You can compare then compare 'shabuffer'
directly to the hashes of each block you have hopefully already assigned as part
of the 'casc_file_data' struct
*/
if (memcmp((&(&casc_file_data->blocks[i])->hash)->x, shabuffer, 32) == 0) {
(&casc_file_data->blocks[i])->completed = 1;
} else {
(&casc_file_data->blocks[i])->completed = 0;
}
}
fclose(fp);
@ -314,16 +349,21 @@ void get_block(csc_block_t* block, csc_peer_t peer, unsigned char* hash, char* o
/*
TODO Write the block into the data file
*/
int write_count;
printf("Got block %d. Wrote from %d to %d\n", block->index, block->offset, block->offset+write_count-1);
Close(peer_socket);
fclose(fp);
}
int get_peers_list(csc_peer_t** peers, unsigned char* hash)
int get_peers_list(csc_peer_t** peers, uint8_t hash[32])
{
rio_t rio;
char rio_buf[MAX_LINE];
uint8_t rio_buf[MAX_LINE];
for (int i = 0; i < MAX_LINE ; i++) {
rio_buf[i] = 0;
}
int tracker_socket;
/*
@ -337,10 +377,17 @@ int get_peers_list(csc_peer_t** peers, unsigned char* hash)
request_header.length = htonl(BODY_SIZE);
memcpy(rio_buf, &request_header, HEADER_SIZE);
/*
TODO Complete the peer list request and
HINT The header has been provided above as a guide
*/
struct RequestBody request_body;
strncpy(request_body.hash, hash, 32);
inet_pton(AF_INET, my_ip, &request_body.ip);
request_body.port = atol(my_port);
memcpy(&rio_buf[HEADER_SIZE], &request_body, BODY_SIZE);
for (int i = 0;i<128;i++) {
printf("%02x ",rio_buf[i]);
}
printf("\n");
return 0; // TODO REMOVE
Rio_writen(tracker_socket, rio_buf, MESSAGE_SIZE);
@ -386,11 +433,25 @@ int get_peers_list(csc_peer_t** peers, unsigned char* hash)
HINT Some of the later provided code expects the peers to be stored in the ''peers' variable, which
is an array of 'csc_peer's, as defined in cascade.h
*/
int peercount;
Close(tracker_socket);
return peercount;
}
// int main() {
// csc_file_t* f = csc_parse_file("./tests/shakespeare.10kib.txt.cascade","shakespeare.txt");
// printf("File size: %i\nBlock Size: %i\nBlock Count: %i\nFile hash: ", f->targetsize, f->blocksize, f->blockcount);
// for (int i = 0;i<32;i++) {
// printf("%02x ",(&f->targethash)->x[i]);
// }
// printf("\nFirst block hash: ");
// for (int i = 0;i<32;i++) {
// printf("%02x ",(&(&f->blocks[0])->hash)->x[i]);
// }
// printf("\n");
// }
int main(int argc, char **argv)
{
if (argc != MAIN_ARGNUM + 1)

View File

@ -24,7 +24,7 @@ struct RequestHeader
struct RequestBody
{
char hash[32];
uint8_t hash[32];
struct in_addr ip;
unsigned short port;
};
@ -34,7 +34,7 @@ struct ClientRequest
char protocol[8];
char reserved[12];
uint64_t block_num;
char hash[32];
uint8_t hash[32];
};
struct ClientResponseHeader
@ -111,7 +111,7 @@ int csc_get_peers(csc_ipport_t tracker, csc_hashdata_t cascadehash, csc_ipport_t
*/
int csc_download_block(csc_ipport_t client, csc_hashdata_t cascadehash, uint64_t blockno, uint64_t blocklength, void* buffer);
int get_peers_list(csc_peer_t** peers, unsigned char* hash);
int get_peers_list(csc_peer_t** peers, uint8_t hash[32]);
void get_block(csc_block_t* block, csc_peer_t peer, unsigned char* hash, char* output_file);
#endif

124456
A3/src/shakespeare.txt Normal file

File diff suppressed because it is too large Load Diff

View File