Skip to content

Commit 83a77ac

Browse files
author
skelly
committed
Refactored main.c and added a bit more verbose error handling to check_fractions
1 parent 1ff7bfd commit 83a77ac

File tree

3 files changed

+72
-44
lines changed

3 files changed

+72
-44
lines changed

client/include/fraction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ int check_magic(uint32_t data);
2929
void print_fraction(fraction_t fraction);
3030
void fraction_free(fraction_t *fraction);
3131
int compare_fractions(const void* a, const void* b);
32-
void check_fractions(fraction_t *fraction, size_t size);
32+
int check_fractions(fraction_t *fraction, size_t size);
3333
#endif

client/src/fraction.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,15 @@ uint32_t calc_crc(fraction_t *frac){
133133
return calculated_crc == frac->crc;
134134
}
135135

136-
void check_fractions(fraction_t *fraction, size_t size){
136+
int check_fractions(fraction_t *fraction, size_t size){
137137
for(size_t i = 0; i < size; i++){
138-
calc_crc(&fraction[i]);
138+
if (!calc_crc(&fraction[i])) {
139+
fprintf(stderr, "Failed to validate integrity of fraction:\n");
140+
print_fraction(fraction[i]);
141+
return 1;
142+
}
139143
}
144+
return 0;
140145
}
141146

142147
void fraction_free(fraction_t *fraction) {

client/src/main.c

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,107 +7,130 @@
77
#include "../include/sock.h"
88
#include "../include/utils.h"
99

10-
/* Networking constants */
1110
#define SERVER_IP "127.0.0.1"
1211
#define SERVER_PORT "8000"
1312

13+
/* Helper functions to assist with cleanup, I hate cleanup */
14+
static void cleanup_char_array(char **array, int n_elem) {
15+
for (int i = 0; i < n_elem; i++) {
16+
free(array[i]);
17+
}
18+
free(array);
19+
}
20+
21+
static void cleanup_fraction_array(fraction_t *array, int n_elem) {
22+
for (int i = 0; i < n_elem; i++) {
23+
fraction_free(&array[i]);
24+
}
25+
free(array);
26+
}
27+
1428
int main(void) {
1529
struct addrinfo hints, *ainfo;
16-
int sfd; // socket file descriptor
30+
int sfd = -1; // to be extra professional
1731
http_res_t http_fraction_res, http_post_res;
18-
/* Setup socket and initiate connection with the server */
32+
char **fraction_links = NULL;
33+
fraction_t *fractions = NULL;
34+
1935
setup_hints(&hints);
2036

2137
if (h_getaddrinfo(SERVER_IP, SERVER_PORT, &hints, &ainfo) != 0) {
2238
fprintf(stderr, "Failed to resolve server address\n");
2339
return EXIT_FAILURE;
2440
}
2541

26-
2742
printf("Connecting to: %s:%s\n", SERVER_IP, SERVER_PORT);
2843
sfd = create_sock_and_conn(ainfo);
44+
freeaddrinfo(ainfo);
2945
if (sfd == -1) {
3046
fprintf(stderr, "Failed to create socket and connect\n");
3147
return EXIT_FAILURE;
3248
}
33-
freeaddrinfo(ainfo); // ainfo no longer needed
3449

35-
/* Get the fraction links */
3650
if (http_get(sfd, "/", &http_fraction_res) != HTTP_SUCCESS) {
3751
fprintf(stderr, "Failed to retrieve fraction links\n");
38-
goto cleanup_socket;
52+
goto cleanup;
3953
}
4054

41-
// Count number of links
4255
int num_links = count_lines(http_fraction_res.data) + 1;
43-
44-
// Allocate memory for fraction links
45-
char **fraction_links = malloc(num_links * sizeof(char *));
56+
fraction_links = malloc(num_links * sizeof(char *));
4657
if (!fraction_links) {
4758
fprintf(stderr, "Failed to allocate memory for fraction links\n");
4859
http_free(&http_fraction_res);
49-
goto cleanup_socket;
60+
goto cleanup;
5061
}
5162

52-
// Split the response data into lines
5363
int lines_read =
5464
split_fraction_links(http_fraction_res.data, fraction_links, num_links);
5565
if (lines_read < 0) {
5666
fprintf(stderr, "Failed to split fraction links\n");
57-
free(fraction_links);
67+
cleanup_char_array(fraction_links, num_links);
5868
http_free(&http_fraction_res);
59-
goto cleanup_socket;
69+
goto cleanup;
6070
}
6171

62-
// Storing the fractions in a array
63-
fraction_t *fractions = malloc(lines_read * sizeof(fraction_t));
64-
if (fractions == NULL) {
65-
fprintf(stderr, "Failed to malloc memory for fractions\n");
72+
73+
fractions = malloc(lines_read * sizeof(fraction_t));
74+
if (!fractions) {
75+
fprintf(stderr, "Failed to allocate memory for fractions\n");
76+
cleanup_char_array(fraction_links, num_links);
77+
http_free(&http_fraction_res);
78+
http_free(&http_post_res);
79+
goto cleanup;
6680
}
6781

68-
for (int i=0; i<lines_read; i++) {
82+
for (int i = 0; i < lines_read; i++) {
6983
if (download_fraction(sfd, fraction_links[i], &fractions[i]) != 0) {
70-
fprintf(stderr, "Failed to parse fraction\n");
84+
fprintf(stderr, "Failed to download fraction\n");
7185
}
7286
}
7387

74-
// Sort the fractions based on index
7588
qsort(fractions, lines_read, sizeof(fraction_t), compare_fractions);
7689
for (int i = 0; i < lines_read; i++) {
7790
print_fraction(fractions[i]);
7891
}
7992

80-
check_fractions(fractions, lines_read);
93+
if (check_fractions(fractions, lines_read)) { // if this works, s0s4 and skelly is to blame!
94+
fprintf(stderr, "Fractions check failed\n");
95+
cleanup_char_array(fraction_links, num_links);
96+
cleanup_fraction_array(fractions, lines_read);
97+
http_free(&http_fraction_res);
98+
http_free(&http_post_res);
99+
goto cleanup;
100+
}
81101

82-
/* Notify the server that we successfully downloaded the fractions */
83102
if (http_post(sfd, "/deadbeef", "plain/text", "{'downloaded':true}",
84103
&http_post_res) != HTTP_SUCCESS) {
85104
fprintf(stderr, "Failed to send POST request\n");
86-
free(fraction_links);
105+
cleanup_char_array(fraction_links, num_links);
87106
http_free(&http_fraction_res);
88-
http_free(&http_post_res);
89-
goto cleanup_socket;
107+
goto cleanup;
90108
}
91109

92-
/* Cleanup */
93110
http_free(&http_fraction_res);
94111
http_free(&http_post_res);
95-
96-
// Free fractions and links
97-
for (int i = 0; i < lines_read; i++) {
98-
free(fraction_links[i]);
99-
fraction_free(&fractions[i]);
100-
}
101-
free(fraction_links);
102-
free(fractions);
112+
cleanup_char_array(fraction_links, num_links);
113+
cleanup_fraction_array(fractions, lines_read);
103114

104115
close(sfd);
105116
return EXIT_SUCCESS;
106117

107-
108-
109-
110-
cleanup_socket:
111-
close(sfd);
118+
/* There's nothing to see here, move on*/
119+
cleanup: // we accept NO comments on this. have a !nice day
120+
if (sfd != -1) {
121+
close(sfd);
122+
}
123+
if (fraction_links) {
124+
cleanup_char_array(fraction_links, num_links);
125+
}
126+
if (fractions) {
127+
cleanup_fraction_array(fractions, num_links);
128+
}
129+
if (http_fraction_res.data) {
130+
http_free(&http_fraction_res);
131+
}
132+
if (http_post_res.data) {
133+
http_free(&http_post_res);
134+
}
112135
return EXIT_FAILURE;
113136
}

0 commit comments

Comments
 (0)