networking/interface.c: huke remaining big statics; use malloc for INET[6]_rresolve
return value. Went thru callers and adjusted them - code got smaller too.
function old new delta
ip_port_str - 126 +126
INET6_rresolve 165 182 +17
static.cache 20 24 +4
route_main 2092 2091 -1
INET_sprint 61 59 -2
INET_nn 4 - -4
INET6_sprint 59 53 -6
udp_do_one 518 508 -10
tcp_do_one 433 423 -10
raw_do_one 494 484 -10
traceroute_main 4117 4105 -12
INET_rresolve 334 321 -13
bb_displayroutes 494 456 -38
snprint_ip_port 244 - -244
static.buff 264 16 -248
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 2/10 up/down: 147/-598) Total: -451 bytes
size busybox_old busybox_unstripped
text data bss dec hex filename
751073 3048 14688 768809 bbb29 busybox_old
750873 3048 14440 768361 bb969 busybox_unstripped
diff --git a/networking/netstat.c b/networking/netstat.c
index d6c10b0..746e250 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -120,50 +120,33 @@
((struct sockaddr *) localaddr)->sa_family = AF_INET;
}
-static const char *get_sname(int port, const char *proto, int num)
+static const char *get_sname(int port, const char *proto, int numeric)
{
- /* hummm, we return static buffer here!! */
- const char *str = itoa(ntohs(port));
- if (!num) {
+ if (!port)
+ return "*";
+ if (!numeric) {
struct servent *se = getservbyport(port, proto);
if (se)
- str = se->s_name;
+ return se->s_name;
}
- if (!port) {
- str = "*";
- }
- return str;
+ /* hummm, we may return static buffer here!! */
+ return itoa(ntohs(port));
}
-static void snprint_ip_port(char *ip_port, int size, struct sockaddr *addr, int port, const char *proto, int numeric)
+static char *ip_port_str(struct sockaddr *addr, int port, const char *proto, int numeric)
{
- const char *port_name;
- int max_len;
- int port_name_len;
+ enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) };
+ char *host, *host_port;
-// TODO: replace by xmalloc_sockaddr2host?
-#if ENABLE_FEATURE_IPV6
- if (addr->sa_family == AF_INET6) {
- INET6_rresolve(ip_port, size, (struct sockaddr_in6 *)addr,
- (numeric & NETSTAT_NUMERIC) ? 0x0fff : 0);
- } else
-#endif
- {
- INET_rresolve(ip_port, size, (struct sockaddr_in *)addr,
- 0x4000 | ((numeric & NETSTAT_NUMERIC) ? 0x0fff : 0),
- 0xffffffff);
- }
- port_name = get_sname(htons(port), proto, numeric);
+ /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6,
+ * while "0.0.0.0" is not. */
- max_len = (option_mask32 & OPT_widedisplay)
- ? (PRINT_IP_MAX_SIZE_WIDE - 1)
- : (PRINT_IP_MAX_SIZE - 1);
- port_name_len = strlen(port_name);
- if ((strlen(ip_port) + port_name_len) > max_len)
- ip_port[max_len - port_name_len] = '\0';
- ip_port += strlen(ip_port);
- *ip_port++ = ':';
- strcpy(ip_port, port_name);
+ host = numeric ? xmalloc_sockaddr2dotted_noport(addr, salen)
+ : xmalloc_sockaddr2host_noport(addr, salen);
+
+ host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
+ free(host);
+ return host_port;
}
static void tcp_do_one(int lnr, const char *line)
@@ -206,14 +189,16 @@
if ((rem_port && (flags & NETSTAT_CONNECTED))
|| (!rem_port && (flags & NETSTAT_LISTENING))
) {
- snprint_ip_port(local_addr, sizeof(local_addr),
+ char *l = ip_port_str(
(struct sockaddr *) &localaddr, local_port,
"tcp", flags & NETSTAT_NUMERIC);
- snprint_ip_port(rem_addr, sizeof(rem_addr),
+ char *r = ip_port_str(
(struct sockaddr *) &remaddr, rem_port,
"tcp", flags & NETSTAT_NUMERIC);
printf(net_conn_line,
- "tcp", rxq, txq, local_addr, rem_addr, tcp_state[state]);
+ "tcp", rxq, txq, l, r, tcp_state[state]);
+ free(l);
+ free(r);
}
}
@@ -285,14 +270,16 @@
if ((have_remaddr && (flags & NETSTAT_CONNECTED))
|| (!have_remaddr && (flags & NETSTAT_LISTENING))
) {
- snprint_ip_port(local_addr, sizeof(local_addr),
+ char *l = ip_port_str(
(struct sockaddr *) &localaddr, local_port,
"udp", flags & NETSTAT_NUMERIC);
- snprint_ip_port(rem_addr, sizeof(rem_addr),
+ char *r = ip_port_str(
(struct sockaddr *) &remaddr, rem_port,
"udp", flags & NETSTAT_NUMERIC);
printf(net_conn_line,
- "udp", rxq, txq, local_addr, rem_addr, state_str);
+ "udp", rxq, txq, l, r, state_str);
+ free(l);
+ free(r);
}
}
}
@@ -339,14 +326,16 @@
if ((have_remaddr && (flags & NETSTAT_CONNECTED))
|| (!have_remaddr && (flags & NETSTAT_LISTENING))
) {
- snprint_ip_port(local_addr, sizeof(local_addr),
+ char *l = ip_port_str(
(struct sockaddr *) &localaddr, local_port,
"raw", flags & NETSTAT_NUMERIC);
- snprint_ip_port(rem_addr, sizeof(rem_addr),
+ char *r = ip_port_str(
(struct sockaddr *) &remaddr, rem_port,
"raw", flags & NETSTAT_NUMERIC);
printf(net_conn_line,
- "raw", rxq, txq, local_addr, rem_addr, itoa(state));
+ "raw", rxq, txq, l, r, itoa(state));
+ free(l);
+ free(r);
}
}
}