Formatting

This commit is contained in:
akp 2023-11-12 00:56:44 +00:00
parent 1ae1950e3d
commit 322904f6ea
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
5 changed files with 121 additions and 124 deletions

View file

@ -1,12 +1,12 @@
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#define USAGE "usage: %s IP PORT OPCODE [ARGS...]\n"
int main (int argc, char **argv) {
int main(int argc, char **argv) {
if (argc < 4) {
fprintf(stderr, USAGE, argv[0]);
return 1;
@ -24,36 +24,36 @@ int main (int argc, char **argv) {
char *payload = NULL;
switch (opcode[0]) {
case 'A':
case 'D':
case 'C': {
if (argc < 6) {
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, "not enough arguments for opcode %s\n", opcode);
return 1;
}
int payload_len = strlen(ip) + strlen(port) + 1;
payload = (char *) malloc(sizeof(char) * (payload_len + 1));
memset(payload, 0, payload_len + 1);
strcat(payload, argv[4]);
if (opcode[0] == 'C') {
strcat(payload, ",");
} else {
strcat(payload, " ");
}
strcat(payload, argv[5]);
}
case 'L': {
break;
}
default: {
fprintf(stderr, "invalid opcode\n");
case 'A':
case 'D':
case 'C': {
if (argc < 6) {
fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, "not enough arguments for opcode %s\n", opcode);
return 1;
}
int payload_len = strlen(ip) + strlen(port) + 1;
payload = (char *)malloc(sizeof(char) * (payload_len + 1));
memset(payload, 0, payload_len + 1);
strcat(payload, argv[4]);
if (opcode[0] == 'C') {
strcat(payload, ",");
} else {
strcat(payload, " ");
}
strcat(payload, argv[5]);
}
case 'L': {
break;
}
default: {
fprintf(stderr, "invalid opcode\n");
return 1;
}
}
unsigned char payload_size = 0;
@ -69,9 +69,7 @@ int main (int argc, char **argv) {
free(payload);
}
// fwrite(message, 1, 2 + payload_size, stdout);
// fwrite(message, 1, 2 + payload_size, stdout);
struct addrinfo addr_spec;
struct addrinfo *addrinfo_res;
@ -82,11 +80,13 @@ int main (int argc, char **argv) {
int addrinfo_status = getaddrinfo(ip, port, &addr_spec, &addrinfo_res);
if (addrinfo_status != 0) {
fprintf(stderr, "get address info: %s\n", gai_strerror(addrinfo_status));
fprintf(stderr, "get address info: %s\n",
gai_strerror(addrinfo_status));
return 1;
}
int sd = socket(addrinfo_res->ai_family, addrinfo_res->ai_socktype, addrinfo_res->ai_protocol);
int sd = socket(addrinfo_res->ai_family, addrinfo_res->ai_socktype,
addrinfo_res->ai_protocol);
if (sd == -1) {
perror("failed to open socket");

View file

@ -1,7 +1,7 @@
#include <stdio.h>
#include "server_includes/sockets.c"
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#define ADDR "::"
@ -31,7 +31,7 @@ void sigint_handler(int) {
}
}
int main (int argc, char **argv) {
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s PORT\n", argv[0]);
return 1;
@ -39,19 +39,14 @@ int main (int argc, char **argv) {
char *port_str = argv[1];
pthread_t thread_id;
pthread_create(
&thread_id,
NULL,
(void *) &run_server,
&port_str
);
pthread_create(&thread_id, NULL, (void *)&run_server, &port_str);
fprintf(stderr, "Alive on %s:%s\n", ADDR, port_str);
signal(SIGINT, &sigint_handler);
int socket_res, status;
if ((status = pthread_join(thread_id, (void *) &socket_res)) != 0) {
if ((status = pthread_join(thread_id, (void *)&socket_res)) != 0) {
fprintf(stderr, "join worker thread: %s\n", strerror(status));
return 1;
}

View file

@ -3,9 +3,9 @@
//
#include "../../firewall_parser.c"
#include <stdlib.h>
#include <stdio.h>
#include "persist.c"
#include <stdio.h>
#include <stdlib.h>
#define HANDLER_OK 0
#define HANDLER_REJECTED 1
@ -17,34 +17,37 @@
int handle_add_rule(char *data, size_t len) {
// Add rule should provide a rule as per exercise 1 definitions.
int retval = HANDLER_OK;
struct Rule *parse_res = (struct Rule*)malloc(sizeof(struct Rule));
struct Rule *parse_res = (struct Rule *)malloc(sizeof(struct Rule));
char *eptr = parse_rule(data, parse_res);
if (eptr == NULL) {
retval = HANDLER_INVRULE;
goto end;
} else if ((eptr - data) < len) {
// This means we've not consumed everything which means that there's extraneous data?
// This means we've not consumed everything which means that there's
// extraneous data?
retval = HANDLER_INVRULE;
goto end;
}
add_frame(parse_res);
end:
free(parse_res); // this is intentionally NOT free_rule as the optional end IP and port assignments are still used elsewhere.
free(parse_res); // this is intentionally NOT free_rule as the optional end
// IP and port assignments are still used elsewhere.
return retval;
}
int handle_delete_rule(char *data, size_t len) {
int retval = HANDLER_OK;
struct Rule *parse_res = (struct Rule*)malloc(sizeof(struct Rule));
struct Rule *parse_res = (struct Rule *)malloc(sizeof(struct Rule));
char *eptr = parse_rule(data, parse_res);
if (eptr == NULL) {
retval = HANDLER_INVRULE;
goto end;
} else if ((eptr - data) < len) {
// This means we've not consumed everything which means that there's extraneous data?
// This means we've not consumed everything which means that there's
// extraneous data?
retval = HANDLER_INVRULE;
goto end;
}
@ -63,7 +66,7 @@ int handle_check_rule(char *data, size_t len) {
struct IP ip;
Port port;
char* endptr = parse_ip(data, &ip);
char *endptr = parse_ip(data, &ip);
if (endptr == NULL) {
return HANDLER_ILLEGALARG;
} else if (endptr[-1] != ',') {
@ -85,6 +88,4 @@ int handle_check_rule(char *data, size_t len) {
}
// Note: this function does not follow the usual return value conventions.
int handle_list_rules(int sd) {
return list_rules(sd);
}
int handle_list_rules(int sd) { return list_rules(sd); }

View file

@ -3,8 +3,8 @@
//
#include "../../firewall_types.c"
#include <string.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
struct LogFrame {
@ -19,11 +19,11 @@ struct Frame {
struct LogFrame *log;
};
struct Frame* persist_head = NULL;
struct Frame *persist_head = NULL;
pthread_rwlock_t persist_lock = PTHREAD_RWLOCK_INITIALIZER;
struct Frame** get_last_frame() {
struct Frame** cursor = &persist_head;
struct Frame **get_last_frame() {
struct Frame **cursor = &persist_head;
while (1) {
if (*cursor == NULL) {
break;
@ -37,7 +37,8 @@ struct Frame** get_last_frame() {
}
void append_frame(struct LogFrame **log, struct IP *ip, Port port) {
struct LogFrame *new_log_frame = (struct LogFrame*) malloc(sizeof(struct LogFrame));
struct LogFrame *new_log_frame =
(struct LogFrame *)malloc(sizeof(struct LogFrame));
memset(new_log_frame, 0, sizeof(struct LogFrame));
new_log_frame->ip = *ip;
@ -54,11 +55,11 @@ void append_frame(struct LogFrame **log, struct IP *ip, Port port) {
}
}
void add_frame(struct Rule* rule) {
void add_frame(struct Rule *rule) {
pthread_rwlock_wrlock(&persist_lock);
struct Frame** last_frame = get_last_frame();
struct Frame **last_frame = get_last_frame();
struct Frame* new_frame = (struct Frame*) malloc(sizeof(struct Frame));
struct Frame *new_frame = (struct Frame *)malloc(sizeof(struct Frame));
memset(new_frame, 0, sizeof(struct Frame));
if (*last_frame == NULL) {
@ -115,7 +116,8 @@ found:
int send_ip(int sd, struct IP *ip) {
char buf[16];
snprintf(buf, 16,"%d.%d.%d.%d", ip->val[0], ip->val[1], ip->val[2], ip->val[3]);
snprintf(buf, 16, "%d.%d.%d.%d", ip->val[0], ip->val[1], ip->val[2],
ip->val[3]);
return send(sd, buf, strlen(buf), 0);
}
@ -206,7 +208,7 @@ end:
}
// Returns 1 if something has been removed, 0 otherwise.
int delete_frame(struct Rule* rule) {
int delete_frame(struct Rule *rule) {
pthread_rwlock_wrlock(&persist_lock);
struct Frame *previous = NULL;

View file

@ -2,15 +2,15 @@
// Created by akp on 02/11/23.
//
#include <pthread.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "handlers.c"
#include <errno.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int open_socket(const char *ip, const char *port, int *res_sd) {
// get address info
@ -24,11 +24,13 @@ int open_socket(const char *ip, const char *port, int *res_sd) {
int addrinfo_status = getaddrinfo(ip, port, &addr_spec, &addrinfo_res);
if (addrinfo_status != 0) {
fprintf(stderr, "get address info: %s\n", gai_strerror(addrinfo_status));
fprintf(stderr, "get address info: %s\n",
gai_strerror(addrinfo_status));
return 1;
}
int sd = socket(addrinfo_res->ai_family, addrinfo_res->ai_socktype, addrinfo_res->ai_protocol);
int sd = socket(addrinfo_res->ai_family, addrinfo_res->ai_socktype,
addrinfo_res->ai_protocol);
if (sd == -1) {
perror("failed to open socket");
@ -63,7 +65,8 @@ int recv_n(int sd, char *buf, size_t n) {
if (total_received == n) {
break;
} else if (total_received > n) {
fprintf(stderr, "Unrecoverable: too many bytes read in connection.\n");
fprintf(stderr,
"Unrecoverable: too many bytes read in connection.\n");
exit(2);
} else {
buf_ptr += num_bytes;
@ -84,7 +87,7 @@ void handle_connection(int *sdptr) {
fprintf(stderr, "[%d] start\n", sd);
char *leader = (char *) malloc(sizeof(char) * 2);
char *leader = (char *)malloc(sizeof(char) * 2);
if (recv_n(sd, leader, 2) != 0) {
if (errno == EPROTO) {
@ -98,12 +101,13 @@ void handle_connection(int *sdptr) {
char operation = leader[0];
size_t message_length = leader[1];
fprintf(stderr, "[%d] Operation: %c, message length: %zu\n", sd, operation, message_length);
fprintf(stderr, "[%d] Operation: %c, message length: %zu\n", sd, operation,
message_length);
char *data = NULL;
if (message_length != 0) {
data = (char *) malloc(sizeof(char) * message_length);
data = (char *)malloc(sizeof(char) * message_length);
if (recv_n(sd, data, message_length) != 0) {
if (errno == EPROTO) {
@ -119,48 +123,48 @@ void handle_connection(int *sdptr) {
char *response = NULL;
switch (operation) {
case 'A': {
int status = handle_add_rule(data, message_length);
if (status != HANDLER_OK) {
response = "Invalid rule";
} else {
response = "Rule added";
}
break;
case 'A': {
int status = handle_add_rule(data, message_length);
if (status != HANDLER_OK) {
response = "Invalid rule";
} else {
response = "Rule added";
}
case 'C': {
int status = handle_check_rule(data, message_length);
if (status == HANDLER_ILLEGALARG) {
response = "Illegal IP address or port specified";
} else if (status == HANDLER_REJECTED) {
response = "Connection rejected";
} else {
response = "Connection accepted";
}
break;
break;
}
case 'C': {
int status = handle_check_rule(data, message_length);
if (status == HANDLER_ILLEGALARG) {
response = "Illegal IP address or port specified";
} else if (status == HANDLER_REJECTED) {
response = "Connection rejected";
} else {
response = "Connection accepted";
}
case 'L': {
if (handle_list_rules(sd) != 0) {
perror("warning: failed to send data to remote host");
goto data_cleanup;
}
break;
break;
}
case 'L': {
if (handle_list_rules(sd) != 0) {
perror("warning: failed to send data to remote host");
goto data_cleanup;
}
case 'D': {
int status = handle_delete_rule(data, message_length);
if (status == HANDLER_INVRULE) {
response = "Rule invalid";
} else if (status == HANDLER_DELETEDRULE) {
response = "Rule deleted";
} else {
response = "Rule not found";
}
break;
}
default: {
response = "Illegal request";
break;
break;
}
case 'D': {
int status = handle_delete_rule(data, message_length);
if (status == HANDLER_INVRULE) {
response = "Rule invalid";
} else if (status == HANDLER_DELETEDRULE) {
response = "Rule deleted";
} else {
response = "Rule not found";
}
break;
}
default: {
response = "Illegal request";
break;
}
}
if (response != NULL) {
@ -204,12 +208,7 @@ int accept_loop(int sd) {
return 1;
}
pthread_create(
&thread_id,
NULL,
(void *) handle_connection,
&new_sd
);
pthread_create(&thread_id, NULL, (void *)handle_connection, &new_sd);
int status = pthread_detach(thread_id);
if (status != 0) {