changeset 1779:7095e1b7240b

HTTP daemons now take '-a' option to set IP address to listen on
author Adam Chlipala <adam@chlipala.net>
date Sat, 23 Jun 2012 09:46:40 -0400
parents 818d4097e2ed
children 85a87f155e7b
files src/c/http.c
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/c/http.c	Sun Jun 03 11:29:31 2012 -0400
+++ b/src/c/http.c	Sat Jun 23 09:46:40 2012 -0400
@@ -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 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 @@
   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 @@
       }
       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 @@
 
   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");