From 9856de01e47af17d4ada73f01fbd0046a1b3e6fb Mon Sep 17 00:00:00 2001 From: Nikolaj Date: Thu, 4 Nov 2021 12:08:55 +0100 Subject: [PATCH] Changes to original code --- A3/src/Makefile | 7 ++----- A3/src/cascade.c | 22 +++++++++++++++------- A3/src/cascade.h | 4 +++- A3/src/endian.h | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 A3/src/endian.h diff --git a/A3/src/Makefile b/A3/src/Makefile index 138e028..26d44a9 100644 --- a/A3/src/Makefile +++ b/A3/src/Makefile @@ -1,7 +1,7 @@ GCC=gcc -O3 -g -Wall -Wextra -pedantic -std=gnu11 LD_FLAGS= -lpthread -all: peer name_server cascade +all: cascade rebuild: clean all @@ -14,9 +14,6 @@ common.o: common.c common.h cascade: cascade.c cascade.h common.o csapp.o sha256.o $(GCC) $< *.o -o $@ $(LD_FLAGS) -name_server: name_server.c name_server.h common.o csapp.o - $(CC) $(CFLAGS) $< *.o -o $@ $(LD_FLAGS) - sha256.o : sha256.c sha256.h $(CC) $(CFLAGS) -c $< -o $@ @@ -26,4 +23,4 @@ zip: ../src.zip cd .. && zip -r src.zip src/Makefile src/*.c src/*.h src/tests/* clean: - rm -rf *.o peer name_server cascade sha256 vgcore* + rm -rf *.o cascade sha256 vgcore* diff --git a/A3/src/cascade.c b/A3/src/cascade.c index a60c9e6..4d67d65 100644 --- a/A3/src/cascade.c +++ b/A3/src/cascade.c @@ -3,9 +3,14 @@ #include #include #include -#include #include +#ifdef __APPLE__ +#include "./endian.h" +#else +#include +#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); diff --git a/A3/src/cascade.h b/A3/src/cascade.h index ed77627..bc4b284 100644 --- a/A3/src/cascade.h +++ b/A3/src/cascade.h @@ -14,7 +14,6 @@ #define PEER_RESPONSE_HEADER_SIZE 9 #define MAX_LINE 128 - struct RequestHeader { char protocol[4]; @@ -112,4 +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); +void get_block(csc_block_t* block, csc_peer_t peer, unsigned char* hash, char* output_file); + #endif diff --git a/A3/src/endian.h b/A3/src/endian.h new file mode 100644 index 0000000..274be30 --- /dev/null +++ b/A3/src/endian.h @@ -0,0 +1,35 @@ +#ifndef __FINK_ENDIANDEV_PKG_ENDIAN_H__ +#define __FINK_ENDIANDEV_PKG_ENDIAN_H__ 1 + +#include + +/** compatibility header for endian.h + * This is a simple compatibility shim to convert + * BSD/Linux endian macros to the Mac OS X equivalents. + * It is public domain. + * */ + +#ifndef __APPLE__ + #warning "This header file (endian.h) is MacOS X specific.\n" +#endif /* __APPLE__ */ + + +#include + +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) + +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) + +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) + + +#endif /* __FINK_ENDIANDEV_PKG_ENDIAN_H__ */