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

@ -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*

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);

View File

@ -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

35
A3/src/endian.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef __FINK_ENDIANDEV_PKG_ENDIAN_H__
#define __FINK_ENDIANDEV_PKG_ENDIAN_H__ 1
#include <machine/endian.h>
/** 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 <libkern/OSByteOrder.h>
#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__ */