Add flag to disable peer address printing
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 33s

Minor teak in main to make Args const.
This commit is contained in:
Stephen Seo 2024-09-06 16:40:36 +09:00
parent 274f0af887
commit fcad980593
4 changed files with 17 additions and 7 deletions

View file

@ -7,6 +7,7 @@ A simple HTTP/1.1 server written in C.
Usage:
-p <port> | --port <port>
--config=<config_file>
--disable-peer-addr-print
## Before Compiling

View file

@ -25,6 +25,7 @@ void print_usage(void) {
puts("Usage:");
puts(" -p <port> | --port <port>");
puts(" --config=<config_file>");
puts(" --disable-peer-addr-print");
}
Args parse_args(int argc, char **argv) {
@ -45,6 +46,8 @@ Args parse_args(int argc, char **argv) {
++argv;
} else if (strncmp(argv[0], "--config=", 9) == 0 && strlen(argv[0]) > 9) {
args.config_file = argv[0] + 9;
} else if (strcmp(argv[0], "--disable-peer-addr-print") == 0) {
args.flags |= 1;
} else {
puts("ERROR: Invalid args!\n");
print_usage();

View file

@ -18,6 +18,8 @@
#define SEODISPARATE_COM_C_SIMPLE_HTTP_ARG_PARSE_H_
typedef struct Args {
// xxxx xxx0 - enable peer addr print.
// xxxx xxx1 - disable peer addr print.
unsigned short flags;
unsigned short port;
// Does not need to be free'd, this should point to a string in argv.

View file

@ -47,7 +47,7 @@
}
int main(int argc, char **argv) {
Args args = parse_args(argc, argv);
const Args args = parse_args(argc, argv);
if (!args.config_file) {
fprintf(stderr, "ERROR Config file not specified!\n");
@ -110,14 +110,18 @@ int main(int argc, char **argv) {
printf("WARNING: accept: errno %d\n", errno);
} else if (ret >= 0) {
// Received connection, handle it.
printf("Peer connected: addr is ");
for (unsigned int idx = 0; idx < 16; ++idx) {
if (idx % 2 == 0 && idx > 0) {
printf(":");
if ((args.flags & 1) == 0) {
printf("Peer connected: addr is ");
for (unsigned int idx = 0; idx < 16; ++idx) {
if (idx % 2 == 0 && idx > 0) {
printf(":");
}
printf("%02x", peer_info.sin6_addr.s6_addr[idx]);
}
printf("%02x", peer_info.sin6_addr.s6_addr[idx]);
puts("");
} else {
printf("Peer connected.\n");
}
puts("");
int connection_fd = ret;
read_ret = read(connection_fd, recv_buf, C_SIMPLE_HTTP_RECV_BUF_SIZE);
if (read_ret < 0) {