From 1f5acb94b4dfa529d950099c7c1937340f81a274 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sun, 27 Feb 2011 03:44:36 +0100 Subject: [PATCH] tlsproxy.c: Use getopt() to parse command line arguments. --- tlsproxy.c | 83 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/tlsproxy.c b/tlsproxy.c index 8ca12f2..2d16465 100644 --- a/tlsproxy.c +++ b/tlsproxy.c @@ -44,6 +44,8 @@ static char *use_proxy_host; static char *use_proxy_port; +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); @@ -64,31 +66,15 @@ 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; - } + parse_arguments(argc, argv); - port = atoi(argv[5 == argc ? 4 : 1]); + 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 - } - server_socket = socket(PF_INET6, SOCK_STREAM, 0); if (-1 == server_socket) { perror("socket()"); @@ -122,6 +108,10 @@ int main(int argc, char **argv) { #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 for (;;) { @@ -138,6 +128,59 @@ int main(int argc, char **argv) { return EXIT_SUCCESS; } +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) { int server_socket; FILE *client_fd, *server_fd; -- 2.45.2