Commit Diff


commit - d431188c66e521a110f987eed2655cc9dfde9864
commit + 721e2325296b1556eb0d2224ef37b387091dff43
blob - 69df871a931e6a1f0f8a8711ca3100785961db88
blob + aa9a687625b4185962c750cd2190877096e3b043
--- ChangeLog
+++ ChangeLog
@@ -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
blob - 2d4ac4601f743d5d4a1e0028d7c444021a78d82c
blob + a82bd4f6ff8fb55c88165308a35dc104b9a0bec3
--- README.md
+++ README.md
@@ -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
blob - f25285f2f3cd622c97faf877bf5cb4e0a4fe67db
blob + 1a8de5bbdc2bc2bbfa8625092f33ae2fa77ab1d1
--- gmid.1
+++ gmid.1
@@ -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.
blob - 86a900aee4bf93b7273886bbd400b8c54015a4f0
blob + 87a4a622f653d350c159d42ba91c6265129d3fd7
--- gmid.c
+++ gmid.c
@@ -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, cons
 		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, cons
 		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, cons
 
 		/* 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);