Unlike xatou16() the bb_lookup_port() won't exit program if port parsing is 
failed.
But it may return a default_port instead. This is not safe because user 
requested another port.
Instead, we return 0 and caller may gracefully show a message or just pass it 
further.
Since the default_port is always zero we may remove the param

Signed-off-by: Sergey Ponomarev <stok...@gmail.com>
---
 include/libbb.h        |  9 +++++++--
 libbb/xconnect.c       | 11 +++--------
 networking/nc.c        |  4 ++--
 networking/nc_bloaty.c |  6 ++----
 networking/tcpudp.c    |  2 +-
 networking/telnet.c    |  2 +-
 6 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index e000ed8b0..62125b36e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -720,8 +720,13 @@ int setsockopt_keepalive(int fd) FAST_FUNC;
 int setsockopt_broadcast(int fd) FAST_FUNC;
 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;
+/* Resolve port by its number or a service name.
+ * If "port" is a number use it as the port.
+ * If "port" is a name it is looked up in /etc/services,
+ * If it isn't found by a service name return 0
+ * NB: Returns port in host byte order.
+ */
+unsigned bb_lookup_port(const char *port, const char *protocol) FAST_FUNC;
 typedef struct len_and_sockaddr {
        socklen_t len;
        union {
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 5dd9cfd28..5ae642db8 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -113,14 +113,9 @@ void FAST_FUNC xconnect(int s, const struct sockaddr 
*s_addr, socklen_t addrlen)
        }
 }
 
-/* Return port number for a service.
- * If "port" is a number use it as the port.
- * If "port" is a name it is looked up in /etc/services,
- * if it isnt found return default_port
- */
-unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, 
unsigned default_port)
+unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol)
 {
-       unsigned port_nr = default_port;
+       unsigned port_nr = 0;
        if (port) {
                int old_errno;
 
@@ -130,7 +125,7 @@ unsigned FAST_FUNC bb_lookup_port(const char *port, const 
char *protocol, unsign
                port_nr = bb_strtou(port, NULL, 10);
                if (errno || port_nr > 65535) {
                        struct servent *tserv = getservbyname(port, protocol);
-                       port_nr = default_port;
+                       port_nr = 0;
                        if (tserv)
                                port_nr = ntohs(tserv->s_port);
 //FIXME: else: port string was garbage, but we don't report that???
diff --git a/networking/nc.c b/networking/nc.c
index 705b7356a..cb7b813db 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -139,7 +139,7 @@ int nc_main(int argc, char **argv)
                        if (ENABLE_NC_SERVER && opt == 'l')
                                IF_NC_SERVER(do_listen++);
                        else if (ENABLE_NC_SERVER && opt == 'p')
-                               IF_NC_SERVER(lport = bb_lookup_port(optarg, 
"tcp", 0));
+                               IF_NC_SERVER(lport = bb_lookup_port(optarg, 
"tcp"));
                        else if (ENABLE_NC_EXTRA && opt == 'w')
                                IF_NC_EXTRA( wsecs = xatou(optarg));
                        else if (ENABLE_NC_EXTRA && opt == 'i')
@@ -216,7 +216,7 @@ int nc_main(int argc, char **argv)
                                close(sfd);
                } else {
                        cfd = create_and_connect_stream_or_die(argv[0],
-                               argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 
0);
+                               bb_lookup_port(argv[1], "tcp"));
                }
        }
 
diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c
index 25b95246f..f9e06b01d 100644
--- a/networking/nc_bloaty.c
+++ b/networking/nc_bloaty.c
@@ -812,7 +812,7 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
        //if (option_mask32 & OPT_n) /* numeric-only, no DNS lookups */
        //if (option_mask32 & OPT_o) /* hexdump log */
        if (option_mask32 & OPT_p) { /* local source port */
-               o_lport = bb_lookup_port(str_p, o_udpmode ? "udp" : "tcp", 0);
+               o_lport = bb_lookup_port(str_p, o_udpmode ? "udp" : "tcp");
                if (!o_lport)
                        bb_error_msg_and_die("bad local port '%s'", str_p);
        }
@@ -827,9 +827,7 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
 
        if (argv[0]) {
                themaddr = xhost2sockaddr(argv[0],
-                       argv[1]
-                       ? bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp", 0)
-                       : 0);
+                       bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp"));
        }
 
        /* create & bind network socket */
diff --git a/networking/tcpudp.c b/networking/tcpudp.c
index 708e05c2e..abfefc0d2 100644
--- a/networking/tcpudp.c
+++ b/networking/tcpudp.c
@@ -363,7 +363,7 @@ int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
        if (max_per_host)
                G.cc = ipsvd_perhost_init(cmax);
 
-       local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp", 0);
+       local_port = bb_lookup_port(argv[1], tcp ? "tcp" : "udp");
        lsa = xhost2sockaddr(argv[0], local_port);
        argv += 2;
 
diff --git a/networking/telnet.c b/networking/telnet.c
index dc088721b..ed7ee5ec5 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -649,7 +649,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv)
        if (!*argv)
                bb_show_usage();
        host = *argv++;
-       port = *argv ? bb_lookup_port(*argv++, "tcp", 23) : 23;
+       port = *argv ? bb_lookup_port(*argv++, "tcp") : 23;
        if (*argv) /* extra params?? */
                bb_show_usage();
 
-- 
2.30.2

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

Reply via email to