summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-05-10 19:59:42 +0200
committerLeah Neukirchen <leah@vuxu.org>2020-05-10 19:59:42 +0200
commit29f12ceacfb6ddb28fd89301819f2bea27cbf37a (patch)
tree778e2d71dcccd5f797e900d92adca56c0c3feb6d
parent3a48e15c894b85f2a98f2f2cc8e9fc0529e82345 (diff)
downloadhittpd-29f12ceacfb6ddb28fd89301819f2bea27cbf37a.tar.gz
hittpd-29f12ceacfb6ddb28fd89301819f2bea27cbf37a.tar.xz
hittpd-29f12ceacfb6ddb28fd89301819f2bea27cbf37a.zip
add -R to set SO_REUSEPORT
-rw-r--r--hittpd.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/hittpd.c b/hittpd.c
index 87c9b3f..574d0f1 100644
--- a/hittpd.c
+++ b/hittpd.c
@@ -102,6 +102,7 @@ int vhost = 0;
 int quiet = 0;
 int show_index = 1;
 int only_public = 0;
+int reuse_port = 0;
 const char *custom_mimetypes = "";
 
 static int
@@ -891,7 +892,7 @@ main(int argc, char *argv[])
 	char *uds = 0;
 
 	int c;
-        while ((c = getopt(argc, argv, "h:m:p:qu:IHM:PV")) != -1)
+        while ((c = getopt(argc, argv, "h:m:p:qu:IHM:PRV")) != -1)
 		switch (c) {
 		case 'h': host = optarg; break;
 		case 'm': custom_mimetypes = optarg; break;
@@ -902,12 +903,13 @@ main(int argc, char *argv[])
 		case 'H': tilde = 1; break;
 		case 'M': default_mimetype = optarg; break;
 		case 'P': only_public = 1; break;
+		case 'R': reuse_port = 1; break;
 		case 'V': vhost = 1; break;
                 default:
                         fprintf(stderr,
 			    "Usage: %s [-h HOST] [-p PORT] [-u SOCKET] "
 			    "[-m :.ext=mime/type:...] [-M DEFAULT_MIMETYPE] "
-			    "[-IHPVq] [DIRECTORY]\n", argv[0]);
+			    "[-IHPRVq] [DIRECTORY]\n", argv[0]);
                         exit(1);
 		}
 
@@ -972,6 +974,15 @@ main(int argc, char *argv[])
 			exit(111);
 		}
 
+#ifdef SO_REUSEPORT
+		if (reuse_port &&
+		    setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT,
+		    &(int){1}, sizeof (int)) < 0) {
+			perror("setsockopt(SO_REUSEPORT)");
+			exit(111);
+		}
+#endif
+
 		r = bind(listenfd, res->ai_addr, res->ai_addrlen);
 		if (r < 0) {
 			perror("bind");