From 8ec0e49b73bb54f677ca60f9d362cf67d5b9f35c Mon Sep 17 00:00:00 2001 From: S0S4 Date: Wed, 18 Sep 2024 13:24:43 +0200 Subject: [PATCH] [WIP] First part of the CRC, currently not working --- client/client | Bin 0 -> 23656 bytes client/include/crc32.h | 3 +-- client/include/fraction.h | 3 ++- client/src/crc32.c | 2 ++ client/src/fraction.c | 45 +++++++++++++++++++++++++++++++++++++- client/src/main.c | 9 +++++++- 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100755 client/client diff --git a/client/client b/client/client new file mode 100755 index 0000000000000000000000000000000000000000..8de800a4d0c5fe24b12bd7732ba2db903c6bef18 GIT binary patch literal 23656 zcmeHv3v^Re`tM20qZCTZ(<&kv1q87r1w<&KDG)d?6biH;3Wl^v+d!MtJfI*|m|Dje zt2m0{_!wq<@ppWnyb5Al5h*G&6y#A9p`+*|78Q}v>M-X1zWqo}V&nSXyYBt3b=UGh z&Ue1&{`R-O{W$xap1VvlrzYrhiaZjO8x@B2HZl@FD@tj;lu#6-GEPasS+5LIx`5mX zKPhJvR}!`)a6Ji=II1K9l3qGpFldZ|morRJB$OT!B)!OC6qg)s>dsLKl{Ai%UT3-# zxRPch47ia2(`5`O5=yync?Ql`3|wBqM^ZRlLaiRfrhOvkx94&_3DZQq#|bF)C|snM zbR*YGn#J`bY!-o#P{xyDLO*G5Tq4(#kYkZ{iq)$WepCuQ38%m&9THL*bjuZ%3S7mgh>vdo%M zW_NfiGb+c8%@{i-)9uV0#atl2C?zPTrp+o)64oeaaLFgxzW7nt zNk8d&YyX;0low}}XFuEj@uY(_^^=1nL-imXlA(j-h06@5YdwA>!{WmWU?M~xf8H;< z5U`&gSAf*}=VZklhdwzD{jNCl{&DEZapSOEe`#KIP?v1=xK0B4_@ps7le-dY>q>(j6+Y2L;r0Y`WtcR$K%kc&vfMH4RPot zap+gYp)ZU>e+cwm%B6~ajuBTFU)dQax1+*kcX*1G3a`hlRM=cDr%Q2rT#llOl}fS8 zW@9Id)e5O%r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4ab 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;