libbb: new option FEATURE_ETC_SERVICES: if off, /etc/services reads often avoided
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 17 Apr 2018 10:43:54 +0000 (12:43 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 17 Apr 2018 10:43:54 +0000 (12:43 +0200)
In practice, "wget http://host.com/" always uses port 80.
People explicitly set non-standard ports via options or parameters
("telnet 1.2.3.4 567" or "telnet 1.2.3.4 ftp") instead of modifying
/etc/services.

function                                             old     new   delta
telnet_main                                         1466    1464      -2
rdate_main                                           215     198     -17
fakeidentd_main                                      269     252     -17
parse_url                                            459     392     -67
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-103)           Total: -103 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/Config.src
networking/isrv_identd.c
networking/telnet.c
networking/wget.c
util-linux/rdate.c

index 646c58bf23abdffdc65a15c7c44b513731475505..a605c7f03f7c5cbd2ae64e934168fbd3875b74dc 100644 (file)
@@ -640,6 +640,11 @@ int setsockopt_bindtodevice(int fd, const char *iface) FAST_FUNC;
 int bb_getsockname(int sockfd, void *addr, socklen_t addrlen) FAST_FUNC;
 /* NB: returns port in host byte order */
 unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port) FAST_FUNC;
+#if ENABLE_FEATURE_ETC_SERVICES
+# define bb_lookup_std_port(portstr, protocol, portnum) bb_lookup_port(portstr, protocol, portnum)
+#else
+# define bb_lookup_std_port(portstr, protocol, portnum) (portnum)
+#endif
 typedef struct len_and_sockaddr {
        socklen_t len;
        union {
index fdf8bbb28b0c6736e79d6a9ee9a00f871524f9ab..16e16480b120f7643b5d0d1efb1cb7f1fbb3184e 100644 (file)
@@ -76,6 +76,18 @@ config FEATURE_ETC_NETWORKS
        a rarely used feature which allows you to use names
        instead of IP/mask pairs in route command.
 
+config FEATURE_ETC_SERVICES
+       bool "Consult /etc/services even for well-known ports"
+       default n
+       help
+       Look up e.g. "telnet" and "http" in /etc/services file
+       instead of assuming ports 23 and 80.
+       This is almost never necessary (everybody uses standard ports),
+       and it makes sense to avoid reading this file.
+       If you disable this option, in the cases where port is explicitly
+       specified as a service name (e.g. "telnet HOST PORTNAME"),
+       it will still be looked up in /etc/services.
+
 config FEATURE_EDITING
        bool "Command line editing"
        default y
index 133d62a6523a091727dd28aa656bb00129e28137..0c33dde4fccd7abdfbed5d35105505c5924639dc 100644 (file)
@@ -159,7 +159,7 @@ int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
        fd = 0;
        if (!(opt & OPT_inetdwait)) {
                fd = create_and_bind_stream_or_die(bind_address,
-                               bb_lookup_port("identd", "tcp", 113));
+                               bb_lookup_std_port("identd", "tcp", 113));
                xlisten(fd, 5);
        }
 
index 15d6a08d8288408566bc71bef1a6e17a7c22db31..1e6be85bd5a35aea3b7f418bd3b1195c2f8b1578 100644 (file)
@@ -643,7 +643,8 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
        if (!*argv)
                bb_show_usage();
        host = *argv++;
-       port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
+       port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
+               : bb_lookup_std_port("telnet", "tcp", 23);
        if (*argv) /* extra params?? */
                bb_show_usage();
 
index 2650b5384a441ec138548335e6f4a5b7439a73a2..b6c76e9dc4e8f41ac7ad8bc8c1027c7d96f6cfdf 100644 (file)
@@ -505,23 +505,23 @@ static void parse_url(const char *src_url, struct host_info *h)
                *p = '\0';
                h->host = p + 3;
                if (strcmp(url, P_FTP) == 0) {
-                       h->port = bb_lookup_port(P_FTP, "tcp", 21);
+                       h->port = bb_lookup_std_port(P_FTP, "tcp", 21);
                } else
 #if SSL_SUPPORTED
 # if ENABLE_FEATURE_WGET_HTTPS
                if (strcmp(url, P_FTPS) == 0) {
-                       h->port = bb_lookup_port(P_FTPS, "tcp", 990);
+                       h->port = bb_lookup_std_port(P_FTPS, "tcp", 990);
                        h->protocol = P_FTPS;
                } else
 # endif
                if (strcmp(url, P_HTTPS) == 0) {
-                       h->port = bb_lookup_port(P_HTTPS, "tcp", 443);
+                       h->port = bb_lookup_std_port(P_HTTPS, "tcp", 443);
                        h->protocol = P_HTTPS;
                } else
 #endif
                if (strcmp(url, P_HTTP) == 0) {
  http:
-                       h->port = bb_lookup_port(P_HTTP, "tcp", 80);
+                       h->port = bb_lookup_std_port(P_HTTP, "tcp", 80);
                        h->protocol = P_HTTP;
                } else {
                        *p = ':';
index f27294e25d10c97fa749184637293b2524640335..5ec795208050f4c251a546c65a468e2a3a1a1b8c 100644 (file)
@@ -45,7 +45,7 @@ static time_t askremotedate(const char *host)
        alarm(10);
        signal(SIGALRM, socket_timeout);
 
-       fd = create_and_connect_stream_or_die(host, bb_lookup_port("time", "tcp", 37));
+       fd = create_and_connect_stream_or_die(host, bb_lookup_std_port("time", "tcp", 37));
 
        if (safe_read(fd, &nett, 4) != 4)    /* read time from server */
                bb_error_msg_and_die("%s: %s", host, "short read");