]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blobdiff - tlsproxy.c
tlsproxy.c: Free resources when receiving SIGINT.
[tlsproxy/tlsproxy.git] / tlsproxy.c
index 39b68df25632a98e153b1a4fbd2119cfe1b3cc50..157633bf20e348ea1613879b0c3584479aefa652 100644 (file)
@@ -30,6 +30,8 @@
 #include <netdb.h>
 /* strncmp() */
 #include <string.h>
+/* sigaction() */
+#include <signal.h>
 /* poll() */
 #include <poll.h>
 
 #define MAX_REQUEST_LINE 4096
 
 
+/* Server should shut down. Set by SIGINT handler. */
+static volatile int done;
+
 /* Proxy hostname and port if specified on the command line. */
 static char *use_proxy_host;
 static char *use_proxy_port;
 
 
+static void sigint_handler(int signal);
+
+static void parse_arguments(int argc, char **argv);
+static void print_usage(const char *argv);
+
 static void handle_connection(int socket);
 static int read_http_request(FILE *client_fd, char *request, size_t length);
 static void send_close_bad_request(FILE *client_fd);
@@ -64,30 +74,23 @@ int main(int argc, char **argv) {
     int client_socket, server_socket;
     struct sockaddr_in6 server_in;
 
-    if (2 != argc && 5 != argc) {
-        printf("Usage: %s [-proxy hostname port] port\n", argv[0]);
-        return EXIT_FAILURE;
-    }
+    struct sigaction action;
 
-    port = atoi(argv[5 == argc ? 4 : 1]);
+    parse_arguments(argc, argv);
+
+    port = atoi(argv[argc - 1]);
     if (0 >= port || 0xffff < port) {
-        printf("Usage: %s [-proxy hostname port] port\n", argv[0]);
-        printf("\n");
-        printf("Invalid port: %s!\n", argv[5 == argc ? 4 : 1]);
+        print_usage(argv[0]);
+        fprintf(stderr, "\ninvalid port");
         return EXIT_FAILURE;
     }
 
-    if (5 == argc) {
-        use_proxy_host = strdup(argv[2]);
-        use_proxy_port = strdup(argv[3]);
-        if (NULL == use_proxy_host || NULL == use_proxy_port) {
-            perror("strdup()");
-            return EXIT_FAILURE;
-        }
-#ifdef DEBUG
-        printf("Using proxy: %s:%s.\n", use_proxy_host, use_proxy_port);
-#endif
-    }
+    /* Setup our SIGINT signal handler which allows a "normal" termination of
+     * the server. */
+    sigemptyset(&action.sa_mask);
+    action.sa_handler = sigint_handler;
+    action.sa_flags   = 0;
+    sigaction(SIGINT, &action, NULL);
 
     server_socket = socket(PF_INET6, SOCK_STREAM, 0);
     if (-1 == server_socket) {
@@ -120,18 +123,90 @@ int main(int argc, char **argv) {
         return EXIT_FAILURE;
     }
 
-    for (;;) {
+#ifdef DEBUG
+    printf("Listening for connections on port %d.\n", port);
+
+    if (NULL != use_proxy_host && NULL != use_proxy_port) {
+        printf("Using proxy: %s:%s.\n", use_proxy_host, use_proxy_port);
+    }
+#endif
+
+    while (!done) {
         /* Accept new connection. */
         client_socket = accept(server_socket, NULL, NULL);
         if (-1 == client_socket) {
             perror("accept()");
-            return EXIT_FAILURE;
+            break;
         }
 
         handle_connection(client_socket);
     }
 
-    return EXIT_SUCCESS;
+    close(server_socket);
+
+    free(use_proxy_host);
+    free(use_proxy_port);
+
+    return EXIT_FAILURE;
+}
+
+static void sigint_handler(int signal_number) {
+    (void)signal_number;
+
+    done = 1;
+}
+
+static void parse_arguments(int argc, char **argv) {
+    int option;
+
+    while (-1 != (option = getopt(argc, argv, "p:h?"))) {
+        switch (option) {
+            case 'p': {
+                char *position;
+
+                /* -p must have the format host:port. */
+                if (NULL == (position = strchr(optarg, ':'))
+                        || position == optarg
+                        || 0 == strlen(position + 1)
+                        || 0 >= atoi(position + 1)
+                        || 0xffff < atoi(position + 1)) {
+                    fprintf(stderr, "-p host:port\n");
+                    exit(EXIT_FAILURE);
+                }
+
+                use_proxy_host = malloc((size_t)(position - optarg) + 1);
+                if (NULL == use_proxy_host) {
+                    perror("malloc()");
+                    exit(EXIT_FAILURE);
+                }
+                memcpy(use_proxy_host, optarg, (size_t)(position - optarg));
+                use_proxy_host[position - optarg] = '\0';
+
+                use_proxy_port = malloc(strlen(position + 1) + 1);
+                if (NULL == use_proxy_port) {
+                    perror("malloc()");
+                    exit(EXIT_FAILURE);
+                }
+                strcpy(use_proxy_port, position + 1);
+
+                break;
+            }
+            case 'h':
+            default: /* '?' */
+                print_usage(argv[0]);
+                exit(EXIT_FAILURE);
+        }
+    }
+
+    if (optind >= argc) {
+        print_usage(argv[0]);
+        exit(EXIT_FAILURE);
+    }
+}
+static void print_usage(const char *argv) {
+    fprintf(stderr, "Usage: %s [-p host:port] port\n", argv);
+    fprintf(stderr, "\n");
+    fprintf(stderr, "-p proxy hostname and port\n");
 }
 
 static void handle_connection(int client_socket) {