[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 1/4] httpd and telnetd: Make default port configurable
From:       Sergey Ponomarev <stokito () gmail ! com>
Date:       2021-08-25 20:12:37
Message-ID: 20210825201240.98752-1-stokito () gmail ! com
[Download RAW message or body]

BusyBox on Termux can't use ports less than 1024 it's patched to change default port \
for httpd to 8080 and telnetd to 8023.

https://github.com/termux/termux-packages/blob/master/packages/busybox/0011-networking-telnetd-default-port.patch
 https://github.com/termux/termux-packages/blob/master/packages/busybox/0010-networking-httpd-default-port.patch


To avoid such patches we can make port configurable.
The macros STR(s) is copied from ftpd.c and it's needed to convert int to string.

For httpd.c made a refactoring of openServer():
* bind_addr_or_port is not preset with "80" and user not specified -p then it will \
                remain a NULL
* Extracted variable fd instead of reusing n var
* 0xffff replaced with 65535 as it done in other places

function                                             old     new   delta
httpd_main                                           668     677      +9
packed_usage                                        2699    2701      +2
.rodata                                            11051   11048      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 11/-3)               Total: 8 bytes
   text    data     bss     dec     hex filename
 186936    4027   69256  260219   3f87b busybox_old
 186944    4027   69256  260227   3f883 busybox_unstripped

Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
---
 networking/httpd.c   | 31 ++++++++++++++++++++++---------
 networking/telnetd.c | 16 ++++++++++++++--
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/networking/httpd.c b/networking/httpd.c
index 568b5f72b..aff8f994e 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -102,6 +102,13 @@
 //config:	help
 //config:	HTTP server.
 //config:
+//config:config HTTPD_PORT_DEFAULT
+//config:	int "Default port"
+//config:	default 80
+//config:	range 1 65535
+//config:	help
+//config:	Port to listen on
+//config:
 //config:config FEATURE_HTTPD_RANGES
 //config:	bool "Support 'Ranges:' header"
 //config:	default y
@@ -257,6 +264,8 @@
 
 //kbuild:lib-$(CONFIG_HTTPD) += httpd.o
 
+//usage:#define STR1(s) #s
+//usage:#define STR(s) STR1(s)
 //usage:#define httpd_trivial_usage
 //usage:       "[-ifv[v]]"
 //usage:       " [-c CONFFILE]"
@@ -270,7 +279,7 @@
 //usage:     "\n	-i		Inetd mode"
 //usage:     "\n	-f		Don't daemonize"
 //usage:     "\n	-v[v]		Verbose"
-//usage:     "\n	-p [IP:]PORT	Bind to IP:PORT (default *:80)"
+//usage:     "\n	-p [IP:]PORT	Bind to IP:PORT. Default " \
STR(CONFIG_HTTPD_PORT_DEFAULT)  //usage:	IF_FEATURE_HTTPD_SETUID(
 //usage:     "\n	-u USER[:GRP]	Set uid/gid after binding to port")
 //usage:	IF_FEATURE_HTTPD_BASIC_AUTH(
@@ -542,7 +551,7 @@ enum {
 	SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
 	IF_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
 	IF_FEATURE_HTTPD_RANGES(range_start = -1;) \
-	bind_addr_or_port = "80"; \
+	bind_addr_or_port = NULL; \
 	index_page = index_html; \
 	file_size = -1; \
 } while (0)
@@ -1030,13 +1039,17 @@ static void decodeBase64(char *data)
  */
 static int openServer(void)
 {
-	unsigned n = bb_strtou(bind_addr_or_port, NULL, 10);
-	if (!errno && n && n <= 0xffff)
-		n = create_and_bind_stream_or_die(NULL, n);
-	else
-		n = create_and_bind_stream_or_die(bind_addr_or_port, 80);
-	xlisten(n, 9);
-	return n;
+	int fd;
+	if (bind_addr_or_port != NULL) {
+		unsigned port = bb_strtou(bind_addr_or_port, NULL, 10);
+		if (!errno && port && port <= 65535) // PORT
+			fd = create_and_bind_stream_or_die(NULL, port);
+		else // IP:PORT
+			fd = create_and_bind_stream_or_die(bind_addr_or_port, CONFIG_HTTPD_PORT_DEFAULT);
+	} else // Nothing specified, use default port
+		fd = create_and_bind_stream_or_die(NULL, CONFIG_HTTPD_PORT_DEFAULT);
+	xlisten(fd, 9);
+	return fd;
 }
 
 /*
diff --git a/networking/telnetd.c b/networking/telnetd.c
index de4d733f9..af83ba3d4 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -68,6 +68,14 @@
 //config:	help
 //config:	Selecting this will make telnetd able to run standalone.
 //config:
+//config:config TELNETD_PORT_DEFAULT
+//config:	int "Default port"
+//config:	default "23"
+//config:	range 1 65535
+//config:	depends on FEATURE_TELNETD_STANDALONE
+//config:	help
+//config:	Port to listen on
+//config:
 //config:config FEATURE_TELNETD_INETD_WAIT
 //config:	bool "Support -w SEC option (inetd wait mode)"
 //config:	default y
@@ -93,6 +101,8 @@
 
 //kbuild:lib-$(CONFIG_TELNETD) += telnetd.o
 
+//usage:#define STR1(s) #s
+//usage:#define STR(s) STR1(s)
 //usage:#define telnetd_trivial_usage
 //usage:       "[OPTIONS]"
 //usage:#define telnetd_full_usage "\n\n"
@@ -103,7 +113,7 @@
 //usage:     "\n	-K		Close connection as soon as login exits"
 //usage:     "\n			(normally wait until all programs close slave pty)"
 //usage:	IF_FEATURE_TELNETD_STANDALONE(
-//usage:     "\n	-p PORT		Port to listen on"
+//usage:     "\n	-p PORT		Port to listen on. Default " \
STR(CONFIG_TELNETD_PORT_DEFAULT)  //usage:     "\n	-b ADDR[:PORT]	Address to bind to"
 //usage:     "\n	-F		Run in foreground"
 //usage:     "\n	-i		Inetd mode"
@@ -708,9 +718,11 @@ int telnetd_main(int argc UNUSED_PARAM, char **argv)
 	} else {
 		master_fd = 0;
 		if (!(opt & OPT_WAIT)) {
-			unsigned portnbr = 23;
+			unsigned portnbr;
 			if (opt & OPT_PORT)
 				portnbr = xatou16(opt_portnbr);
+			else
+				portnbr = CONFIG_TELNETD_PORT_DEFAULT;
 			master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
 			xlisten(master_fd, 1);
 		}
-- 
2.30.2

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic