comparison src/c/http.c @ 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 ea131de361d9
children 3d922a28370b
comparison
equal deleted inserted replaced
1778:818d4097e2ed 1779:7095e1b7240b
4 #include <string.h> 4 #include <string.h>
5 #include <stdlib.h> 5 #include <stdlib.h>
6 #include <sys/types.h> 6 #include <sys/types.h>
7 #include <sys/socket.h> 7 #include <sys/socket.h>
8 #include <netinet/in.h> 8 #include <netinet/in.h>
9 #include <arpa/inet.h>
9 #include <unistd.h> 10 #include <unistd.h>
10 #include <signal.h> 11 #include <signal.h>
11 #include <stdarg.h> 12 #include <stdarg.h>
12 13
13 #include <pthread.h> 14 #include <pthread.h>
215 216
216 return NULL; 217 return NULL;
217 } 218 }
218 219
219 static void help(char *cmd) { 220 static void help(char *cmd) {
220 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd); 221 printf("Usage: %s [-p <port>] [-a <IP address>] [-t <thread count>]\n", cmd);
221 } 222 }
222 223
223 static void sigint(int signum) { 224 static void sigint(int signum) {
224 printf("Exiting....\n"); 225 printf("Exiting....\n");
225 exit(0); 226 exit(0);
236 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt; 237 int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt;
237 238
238 signal(SIGINT, sigint); 239 signal(SIGINT, sigint);
239 signal(SIGPIPE, SIG_IGN); 240 signal(SIGPIPE, SIG_IGN);
240 241
241 while ((opt = getopt(argc, argv, "hp:t:")) != -1) { 242 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
243 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
244
245 while ((opt = getopt(argc, argv, "hp:a:t:")) != -1) {
242 switch (opt) { 246 switch (opt) {
243 case '?': 247 case '?':
244 fprintf(stderr, "Unknown command-line option"); 248 fprintf(stderr, "Unknown command-line option");
245 help(argv[0]); 249 help(argv[0]);
246 return 1; 250 return 1;
256 help(argv[0]); 260 help(argv[0]);
257 return 1; 261 return 1;
258 } 262 }
259 break; 263 break;
260 264
265 case 'a':
266 if (!inet_pton(AF_INET, optarg, &my_addr.sin_addr)) {
267 fprintf(stderr, "Invalid IP address\n");
268 help(argv[0]);
269 return 1;
270 }
271 break;
272
261 case 't': 273 case 't':
262 nthreads = atoi(optarg); 274 nthreads = atoi(optarg);
263 if (nthreads <= 0) { 275 if (nthreads <= 0) {
264 fprintf(stderr, "Invalid thread count\n"); 276 fprintf(stderr, "Invalid thread count\n");
265 help(argv[0]); 277 help(argv[0]);
289 return 1; 301 return 1;
290 } 302 }
291 303
292 my_addr.sin_family = AF_INET; // host byte order 304 my_addr.sin_family = AF_INET; // host byte order
293 my_addr.sin_port = htons(uw_port); // short, network byte order 305 my_addr.sin_port = htons(uw_port); // short, network byte order
294 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
295 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
296 306
297 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) { 307 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
298 fprintf(stderr, "Listener socket bind failed\n"); 308 fprintf(stderr, "Listener socket bind failed\n");
299 return 1; 309 return 1;
300 } 310 }