make port number configurable

This commit is contained in:
Omar Polo 2020-11-18 09:12:27 +01:00
parent d431188c66
commit 721e232529
No known key found for this signature in database
GPG Key ID: 35F98C96A1786F0D
4 changed files with 36 additions and 5 deletions

View File

@ -1,3 +1,7 @@
2020-11-17 Omar Polo <op@omarpolo.com>
* gmid.c (main): add flag -p to change the port
2020-11-10 Omar Polo <op@omarpolo.com>
* ChangeLog: 1.3 tagged, fixed ChangeLog format

View File

@ -11,6 +11,7 @@
\[**-d**&nbsp;*docs*]
\[**-k**&nbsp;*key.pem*]
\[**-l**&nbsp;*logfile*]
\[**-p**&nbsp;*port*]
\[**-x**&nbsp;*cgi-bin*]
# DESCRIPTION
@ -76,6 +77,10 @@ The options are as follows:
> log to the given file instead of the standard error.
**-p** *port*
> The port to bind to, by default 1965.
**-x** *dir*
> Enable execution of CGI scripts inside the given directory (relative

3
gmid.1
View File

@ -25,6 +25,7 @@
.Op Fl d Ar docs
.Op Fl k Ar key.pem
.Op Fl l Ar logfile
.Op Fl p Ar port
.Op Fl x Ar cgi-bin
.Ek
.Sh DESCRIPTION
@ -79,6 +80,8 @@ The key for the certificate, by default is
.Pa key.pem .
.It Fl l Ar logfile
log to the given file instead of the standard error.
.It Fl p Ar port
The port to bind to, by default 1965.
.It Fl x Ar dir
Enable execution of CGI scripts inside the given directory (relative
to the document root.) Cannot be provided more than once.

29
gmid.c
View File

@ -116,6 +116,7 @@ struct etm { /* file extension to mime */
const char *dir, *cgi;
int dirfd, logfd;
int port;
int connected_clients;
void siginfo_handler(int);
@ -464,7 +465,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
goto err;
case 0: { /* child */
char *ex, *requri;
char *ex, *requri, *portno;
char addr[INET_ADDRSTRLEN];
char *argv[] = { NULL, NULL, NULL };
@ -477,6 +478,9 @@ start_cgi(const char *spath, const char *relpath, const char *query,
if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
goto childerr;
if (asprintf(&portno, "%d", port) == -1)
goto childerr;
if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
goto childerr;
@ -489,7 +493,7 @@ start_cgi(const char *spath, const char *relpath, const char *query,
/* fix the env */
setenv("SERVER_SOFTWARE", "gmid", 1);
setenv("SERVER_PORT", "1965", 1);
setenv("SERVER_PORT", portno, 1);
/* setenv("SERVER_NAME", "", 1); */
setenv("SCRIPT_NAME", spath, 1);
setenv("SCRIPT_EXECUTABLE", ex, 1);
@ -943,7 +947,7 @@ usage(const char *me)
{
fprintf(stderr,
"USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
"[-l logfile] [-x cgi-bin]\n",
"[-l logfile] [-p port] [-x cgi-bin]\n",
me);
}
@ -968,8 +972,9 @@ main(int argc, char **argv)
dir = "docs/";
logfd = 2; /* stderr */
cgi = NULL;
port = 1965;
while ((ch = getopt(argc, argv, "c:d:hk:l:x:")) != -1) {
while ((ch = getopt(argc, argv, "c:d:hk:l:p:x:")) != -1) {
switch (ch) {
case 'c':
cert = optarg;
@ -994,6 +999,20 @@ main(int argc, char **argv)
err(1, "%s", optarg);
break;
case 'p': {
char *ep;
long lval;
errno = 0;
lval = strtol(optarg, &ep, 10);
if (optarg[0] == '\0' || *ep != '\0')
err(1, "not a number: %s", optarg);
if (lval < 0 || lval > UINT16_MAX)
err(1, "port number out of range: %s", optarg);
port = lval;
break;
}
case 'x':
cgi = optarg;
break;
@ -1023,7 +1042,7 @@ main(int argc, char **argv)
if (tls_configure(ctx, conf) == -1)
errx(1, "tls_configure: %s", tls_error(ctx));
sock = make_socket(1965, AF_INET);
sock = make_socket(port, AF_INET);
if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
err(1, "open: %s", dir);