diff --git a/client/client b/client/client new file mode 100755 index 0000000..8de800a Binary files /dev/null and b/client/client differ diff --git a/client/include/crc32.h b/client/include/crc32.h index b20ce31..50bf4ab 100644 --- a/client/include/crc32.h +++ b/client/include/crc32.h @@ -3,7 +3,7 @@ #include #include - +#include "fraction.h" extern const uint32_t crc32_tab[]; /* * A function that calculates the CRC-32 based on the table above is @@ -12,5 +12,4 @@ extern const uint32_t crc32_tab[]; * in sys/libkern.h, where it can be inlined. */ uint32_t crc32(const void *buf, size_t size); - #endif // CRC32_H diff --git a/client/include/fraction.h b/client/include/fraction.h index 52ba967..29fd820 100644 --- a/client/include/fraction.h +++ b/client/include/fraction.h @@ -22,9 +22,10 @@ typedef struct { } fraction_t; int download_fraction(int sfd, char *url, fraction_t *fraction); -int fraction_parse(char *data, size_t size, fraction_t *fraction); +int fraction_parse(char *data, size_t size, fraction_t *fraction); int check_magic(uint32_t data); void print_fraction(fraction_t fraction); void fraction_free(fraction_t *fraction); int compare_fractions(const void* a, const void* b); +void check_fractions(fraction_t *fraction, size_t size); #endif diff --git a/client/src/crc32.c b/client/src/crc32.c index 5c5c57f..d41f49c 100644 --- a/client/src/crc32.c +++ b/client/src/crc32.c @@ -1,4 +1,5 @@ #include "../include/crc32.h" +#include "../include/fraction.h" const uint32_t crc32_tab[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, @@ -54,3 +55,4 @@ uint32_t crc32(const void *buf, size_t size) { crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); return crc ^ ~0U; } + diff --git a/client/src/fraction.c b/client/src/fraction.c index 854257f..c4368af 100644 --- a/client/src/fraction.c +++ b/client/src/fraction.c @@ -1,6 +1,6 @@ #include "../include/fraction.h" #include - +#include "../include/crc32.h" // Change the return type to int to indicate success or failure int download_fraction(int sfd, char *url, fraction_t *fraction) { @@ -109,9 +109,52 @@ void print_fraction(fraction_t fraction) { printf("\n\n"); } +uint32_t calc_crc(fraction_t *frac){ + + uint8_t buffer[sizeof(frac->magic) + sizeof(frac->index) + sizeof(frac->iv) + strlen(frac->data)]; + size_t offset = 0; + + memcpy(buffer + offset, &frac->magic, sizeof(frac->magic)); + offset += sizeof(frac->magic); + + memcpy(buffer + offset, &frac->index, sizeof(frac->index)); + offset += sizeof(frac->index); + + memcpy(buffer + offset, frac->iv, sizeof(frac->iv)); + offset += sizeof(frac->iv); + + memcpy(buffer + offset, frac->data, strlen(frac->data)); + offset += strlen(frac->data); + + uint32_t calculated_crc = crc32(buffer, offset); + + if (calculated_crc == frac->crc) { + printf("Checksum correcto\n"); + } else { + printf("Checksum incorrecto\n"); + printf("Checksum generado: %08X\n", calculated_crc); + printf("Checksum que deberia ser: %08X\n", frac->crc); + } + + return calculated_crc == frac->crc; +} + +void check_fractions(fraction_t *fraction, size_t size){ + + for(size_t i = 0; i < size; i++){ + if(calc_crc(&fraction[i])){ + puts("checksum is correct"); + } else{ + puts("Checksum incorrect"); + } + } +} + void fraction_free(fraction_t *fraction) { free(fraction->data); fraction->magic = 0; fraction->index = 0; fraction->crc = 0; } + + diff --git a/client/src/main.c b/client/src/main.c index c7d57df..084d3e2 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -15,7 +15,7 @@ int main(void) { struct addrinfo hints, *ainfo; int sfd; // socket file descriptor http_res_t http_fraction_res, http_post_res; - + size_t size = 0; /* Setup socket and initiate connection with the server */ setup_hints(&hints); @@ -76,6 +76,7 @@ int main(void) { qsort(fractions, lines_read, sizeof(fraction_t), compare_fractions); for (int i = 0; i < lines_read; i++) { print_fraction(fractions[i]); + size++; } /* Notify the server that we successfully downloaded the fractions */ @@ -88,6 +89,9 @@ int main(void) { goto cleanup_socket; } + check_fractions(fractions,size); + + /* Cleanup */ http_free(&http_fraction_res); http_free(&http_post_res); @@ -103,6 +107,9 @@ int main(void) { close(sfd); return EXIT_SUCCESS; + + + cleanup_socket: close(sfd); return EXIT_FAILURE;