. Changed argument --bind' -> --addr'

. Implemented arg parsing for port, addr, maxconn, log, chroot
. init_sockin() prints the addr:port that sockin is bound to
This commit is contained in:
Emil Mikulic 2003-02-20 10:31:34 +00:00
parent 79e52dfae2
commit 7a8ff37bb3

View File

@ -10,6 +10,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h> #include <err.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -17,13 +18,16 @@
/* defaults can be overridden on the command-line */ /* defaults can be overridden on the command-line */
static u_int32_t bindaddr = INADDR_ANY; static in_addr_t bindaddr = INADDR_ANY;
static u_int16_t bindport = 80; static u_int16_t bindport = 80;
static int max_connections = -1; /* kern.ipc.somaxconn */ static int max_connections = -1; /* kern.ipc.somaxconn */
static int sockin; static int sockin; /* socket to accept connections from */
/*@null@*/ /*@null@*/
static char *wwwroot = NULL; static char *wwwroot = NULL; /* a path name */
/*@null@*/
static char *logfile_name = NULL; /* NULL = no logging */
static int want_chroot = 0;
@ -55,6 +59,8 @@ static void init_sockin(void)
sizeof(struct sockaddr)) == -1) sizeof(struct sockaddr)) == -1)
err(1, "bind(port %u)", bindport); err(1, "bind(port %u)", bindport);
printf("listening on %s:%u\n", inet_ntoa(addrin.sin_addr), bindport);
/* listen on socket */ /* listen on socket */
if (listen(sockin, max_connections) == -1) if (listen(sockin, max_connections) == -1)
err(1, "listen()"); err(1, "listen()");
@ -72,9 +78,9 @@ static void usage(void)
"\t--port number (default: %u)\n" /* DEFAULT_PORT */ "\t--port number (default: %u)\n" /* DEFAULT_PORT */
"\t\tSpecifies which port to listen on for connections.\n" "\t\tSpecifies which port to listen on for connections.\n"
"\n" "\n"
"\t--bind ip (default: all)\n" "\t--addr ip (default: all)\n"
"\t\tIf multiple interfaces are present,\n" "\t\tIf multiple interfaces are present, specifies\n"
"\t\tspecifies which one to bind the listening port to.\n" "\t\twhich one to bind the listening port to.\n"
"\n" "\n"
"\t--maxconn number (default: system maximum)\n" "\t--maxconn number (default: system maximum)\n"
"\t\tSpecifies how many concurrent connections to accept.\n" "\t\tSpecifies how many concurrent connections to accept.\n"
@ -85,7 +91,7 @@ static void usage(void)
"\t--chroot (default: don't chroot)\n" "\t--chroot (default: don't chroot)\n"
"\t\tLocks server into wwwroot directory for added security.\n" "\t\tLocks server into wwwroot directory for added security.\n"
"\n" "\n"
"\t--uid blah, --gid blah\n" /* FIXME */ /* "\t--uid blah, --gid blah\n" FIXME */
, bindport); , bindport);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -105,8 +111,34 @@ static void parse_commandline(const int argc, char *argv[])
/* walk through the remainder of the arguments (if any) */ /* walk through the remainder of the arguments (if any) */
for (i=2; i<argc; i++) for (i=2; i<argc; i++)
{ {
/* FIXME */ if (strcmp(argv[i], "--port") == 0)
errx(1, "unknown argument `%s'", argv[i]); {
if (++i >= argc) errx(1, "missing number after --port");
bindport = (u_int16_t)atoi(argv[i]);
}
else if (strcmp(argv[i], "--addr") == 0)
{
if (++i >= argc) errx(1, "missing ip after --addr");
bindaddr = inet_addr(argv[i]);
if (bindaddr == (in_addr_t)INADDR_NONE)
errx(1, "malformed --addr argument");
}
else if (strcmp(argv[i], "--maxconn") == 0)
{
if (++i >= argc) errx(1, "missing number after --maxconn");
max_connections = atoi(argv[i]);
}
else if (strcmp(argv[i], "--log") == 0)
{
if (++i >= argc) errx(1, "missing filename after --log");
logfile_name = argv[i];
}
else if (strcmp(argv[i], "--chroot") == 0)
{
want_chroot = 1;
}
else
errx(1, "unknown argument `%s'", argv[i]);
} }
} }