Skip to content

Commit

Permalink
Merge pull request #17 from Black-Speedy/CheckSum
Browse files Browse the repository at this point in the history
minor refactoring and added checksum
  • Loading branch information
MikailuReeves authored Dec 21, 2023
2 parents 4840abc + cf81712 commit 16c42e9
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2,019 deletions.
70 changes: 50 additions & 20 deletions WebShare-Connect/client.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "WebShare-Connect.h"
#include "client.h"
#include "sha512.h"

// This is the header for the file
typedef struct {
int64_t file_size;
int chunk_size;
int chunk_size;
unsigned char hash[SHA512_DIGEST_LENGTH];
} file_header_t;

zsock_t* client(void* context, const char *port, int threads)
Expand All @@ -25,23 +27,23 @@ zsock_t* client(void* context, const char *port, int threads)
}

void client_receive(zsock_t* client_sock, const char* output_file_path) {
printf("Receiving file...\n");
printf("\n-- File Reception --\n");

file_header_t header;
size_t header_size = sizeof(file_header_t);
byte* header_buf = NULL;

// Receive the header as a binary blob
// Receive the header
int rc = zsock_recv(client_sock, "b", &header_buf, &header_size);
if (rc == -1) {
fprintf(stderr, "Failed to receive header\n");
fprintf(stderr, "Error: Failed to receive header.\n");
return;
}

// Ensure the received header size matches the expected size
// Validate the header size
if (header_size != sizeof(file_header_t)) {
fprintf(stderr, "Received header size does not match\n");
free(header_buf); // Free the received buffer
fprintf(stderr, "Error: Received header size does not match expected size.\n");
free(header_buf);
return;
}

Expand All @@ -53,21 +55,21 @@ void client_receive(zsock_t* client_sock, const char* output_file_path) {
memcpy(&header, header_buf, sizeof(header));
free(header_buf); // Free the received buffer after copying

printf("Received file header\n");
int64_t file_size = header.file_size;
int chunk_size = header.chunk_size;
printf("Expected file size: %lld bytes\n", file_size);
printf("Chunk size: %d bytes\n", chunk_size);
printf("Header received successfully.\n");
printf("Expected file size: %lld bytes.\n", header.file_size);
printf("Chunk size: %d bytes.\n", header.chunk_size);

char* buffer = (char*)malloc(chunk_size);
if (buffer == NULL) {
fprintf(stderr, "Memory allocation error\n");
char* buffer = (char*)malloc(header.chunk_size);
if (!buffer) {
fprintf(stderr, "Error: Memory allocation failed.\n");
return;
}

FILE* fp = fopen(output_file_path, "wb");
if (fp == NULL) {
fprintf(stderr, "File open error\n");
if (!fp) {
fprintf(stderr, "Error: File open failed.\n");
free(buffer);
return;
}
Expand All @@ -93,13 +95,39 @@ void client_receive(zsock_t* client_sock, const char* output_file_path) {
}
received_size += size;
current_chunk++;
fflush(&buffer);
//printf("Received chunk %d/%d with size %zu bytes\n", current_chunk, chunk_count, size);
printf("Chunk %d/%d received. Size: %d bytes.\n", current_chunk, chunk_count, size);
}

free(buffer);
fclose(fp);
printf("File reception completed.\n");

// Compute hash of received file and compare with the expected hash
unsigned char* expected_hash = header.hash;
unsigned char received_hash[SHA512_DIGEST_LENGTH];
compute_sha512(output_file_path, received_hash); // Compute hash of received file
char received_hash_string[SHA512_DIGEST_LENGTH * 2 + 1];
char expected_hash_string[SHA512_DIGEST_LENGTH * 2 + 1];
convert_hash_to_hex_string(received_hash, received_hash_string, SHA512_DIGEST_LENGTH); // Convert received hash
convert_hash_to_hex_string(expected_hash, expected_hash_string, SHA512_DIGEST_LENGTH); // Convert expected hash

// Compare the raw binary hashes
if (memcmp(received_hash, expected_hash, SHA512_DIGEST_LENGTH) != 0)
{
fprintf(stderr, "Received file hash does not match\n");
printf("Received Hash (binary): ");
for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
{
printf("%02x", received_hash[i]);
}
printf("\nExpected Hash (binary): ");
for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
{
printf("%02x", expected_hash[i]);
}
printf("\n");
}
else {
printf("File hash verified successfully.\n");
}
}


Expand All @@ -125,7 +153,9 @@ int client_main(int argc, char const* argv[]) {

// Receive file
client_receive(client_sock, output_file_path);
printf("File received.\n");
printf("\x1b[32mFile transfer completed successfully.\x1b[32m\n ");
// return to normal color
printf("\x1b[0m");

// Clean up
zsock_destroy(&client_sock);
Expand Down
Loading

0 comments on commit 16c42e9

Please sign in to comment.