]> git.seodisparate.com - c_simple_http/commitdiff
Add flag to disable peer address printing
authorStephen Seo <seo.disparate@gmail.com>
Fri, 6 Sep 2024 07:40:36 +0000 (16:40 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 6 Sep 2024 07:40:36 +0000 (16:40 +0900)
Minor teak in main to make Args const.

README.md
src/arg_parse.c
src/arg_parse.h
src/main.c

index e5ebf22da2ef271390d994e01039aba675e54803..ca5969b09e3fe697ba3dd772b8c27b1f140659d1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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
 
index 0ca73cdeb293168bd14296e377d1ba9f522e2589..4bc3e3b7a3593d281eeab93f28c31bc21d9d0e32 100644 (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();
index 6e762067d54933f82f2b7f3d9abda0faf2d1bfc7..641c29bb0713d1160b743abefb5a9170859f48d2 100644 (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.
index 1a883e701da789c0d7a93368971f3ba4d8578ed4..bac21d6eaa4cc6235a9ecc45a8d8a4a57e7f77ec 100644 (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) {