diff options
author | Adam Chlipala <adam@chlipala.net> | 2012-06-23 09:46:40 -0400 |
---|---|---|
committer | Adam Chlipala <adam@chlipala.net> | 2012-06-23 09:46:40 -0400 |
commit | cdbb09239c8dc840a8146340cc1d3f26d17a9007 (patch) | |
tree | 3eb78e5f5707c86891dd9f8eaa2f38d1aa243eb9 | |
parent | 797db05343b520b16ea4f8eeab5fea6255d3284d (diff) |
HTTP daemons now take '-a' option to set IP address to listen on
-rw-r--r-- | src/c/http.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/c/http.c b/src/c/http.c index 5fd8080e..9af86070 100644 --- a/src/c/http.c +++ b/src/c/http.c @@ -6,6 +6,7 @@ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> +#include <arpa/inet.h> #include <unistd.h> #include <signal.h> #include <stdarg.h> @@ -217,7 +218,7 @@ static void *worker(void *data) { } static void help(char *cmd) { - printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd); + printf("Usage: %s [-p <port>] [-a <IP address>] [-t <thread count>]\n", cmd); } static void sigint(int signum) { @@ -238,7 +239,10 @@ int main(int argc, char *argv[]) { signal(SIGINT, sigint); signal(SIGPIPE, SIG_IGN); - while ((opt = getopt(argc, argv, "hp:t:")) != -1) { + my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP + memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); + + while ((opt = getopt(argc, argv, "hp:a:t:")) != -1) { switch (opt) { case '?': fprintf(stderr, "Unknown command-line option"); @@ -258,6 +262,14 @@ int main(int argc, char *argv[]) { } break; + case 'a': + if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) { + fprintf(stderr, "Invalid IP address\n"); + help(argv[0]); + return 1; + } + break; + case 't': nthreads = atoi(optarg); if (nthreads <= 0) { @@ -291,8 +303,6 @@ int main(int argc, char *argv[]) { my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(uw_port); // short, network byte order - my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP - memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) { fprintf(stderr, "Listener socket bind failed\n"); |