aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/xconnect.c22
1 files changed, 7 insertions, 15 deletions
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index f5c514b2c..65b1cb8de 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -117,27 +117,19 @@ void FAST_FUNC xconnect(int s, const struct sockaddr *saddr, socklen_t addrlen)
117 117
118/* Return port number for a service. 118/* Return port number for a service.
119 * If "port" is a number use it as the port. 119 * If "port" is a number use it as the port.
120 * If "port" is a name it is looked up in /etc/services, 120 * If "port" is a name it is looked up in /etc/services.
121 * if it isnt found return default_port 121 * if NULL, return default_port
122 */ 122 */
123unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned default_port) 123unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned port_nr)
124{ 124{
125 unsigned port_nr = default_port;
126 if (port) { 125 if (port) {
127 int old_errno;
128
129 /* Since this is a lib function, we're not allowed to reset errno to 0.
130 * Doing so could break an app that is deferring checking of errno. */
131 old_errno = errno;
132 port_nr = bb_strtou(port, NULL, 10); 126 port_nr = bb_strtou(port, NULL, 10);
133 if (errno || port_nr > 65535) { 127 if (errno || port_nr > 65535) {
134 struct servent *tserv = getservbyname(port, protocol); 128 struct servent *tserv = getservbyname(port, protocol);
135 port_nr = default_port; 129 if (!tserv)
136 if (tserv) 130 bb_error_msg_and_die("bad port '%s'", port);
137 port_nr = ntohs(tserv->s_port); 131 port_nr = ntohs(tserv->s_port);
138//FIXME: else: port string was garbage, but we don't report that???
139 } 132 }
140 errno = old_errno;
141 } 133 }
142 return (uint16_t)port_nr; 134 return (uint16_t)port_nr;
143} 135}
@@ -241,7 +233,7 @@ IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
241 cp++; /* skip ':' */ 233 cp++; /* skip ':' */
242 port = bb_strtou(cp, NULL, 10); 234 port = bb_strtou(cp, NULL, 10);
243 if (errno || (unsigned)port > 0xffff) { 235 if (errno || (unsigned)port > 0xffff) {
244 bb_error_msg("bad port spec '%s'", org_host); 236 bb_error_msg("bad port '%s'", cp);
245 if (ai_flags & DIE_ON_ERROR) 237 if (ai_flags & DIE_ON_ERROR)
246 xfunc_die(); 238 xfunc_die();
247 return NULL; 239 return NULL;