diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-19 11:12:46 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-19 11:12:46 +0000 |
commit | 6d9ea24611090f06b74e0e5b038b6c5b4bbe1063 (patch) | |
tree | 7fbefb532c3412b0264a9faf7d0ad3f42254b86e /networking | |
parent | 1b16bdaebf7d0e543e048dfec9f34f06e983336c (diff) | |
download | busybox-6d9ea24611090f06b74e0e5b038b6c5b4bbe1063.tar.gz |
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
Diffstat (limited to 'networking')
-rw-r--r-- | networking/interface.c | 13 | ||||
-rw-r--r-- | networking/netstat.c | 79 | ||||
-rw-r--r-- | networking/route.c | 26 | ||||
-rw-r--r-- | networking/traceroute.c | 18 |
4 files changed, 61 insertions, 75 deletions
diff --git a/networking/interface.c b/networking/interface.c index f8721f409..204febf7d 100644 --- a/networking/interface.c +++ b/networking/interface.c @@ -84,13 +84,12 @@ struct in6_ifreq { /* Display an Internet socket address. */ static const char *INET_sprint(struct sockaddr *sap, int numeric) { - static char buff[128]; + static char *buff; + free(buff); if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return "[NONE SET]"; - if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, - numeric, 0xffffff00) != 0) - return NULL; + buff = INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00); return buff; } @@ -167,12 +166,12 @@ static const struct aftype inet_aftype = { /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */ static const char *INET6_sprint(struct sockaddr *sap, int numeric) { - static char buff[128]; + static char *buff; + free(buff); if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return "[NONE SET]"; - if (INET6_rresolve(buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric)) - return "[UNKNOWN]"; + buff = INET6_rresolve((struct sockaddr_in6 *) sap, numeric); return buff; } diff --git a/networking/netstat.c b/networking/netstat.c index d6c10b07b..746e25091 100644 --- a/networking/netstat.c +++ b/networking/netstat.c @@ -120,50 +120,33 @@ static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr) ((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); - - 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); + /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6, + * while "0.0.0.0" is not. */ + + 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 @@ static void tcp_do_one(int lnr, const char *line) 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 @@ static void udp_do_one(int lnr, const char *line) 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 @@ static void raw_do_one(int lnr, const char *line) 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); } } } diff --git a/networking/route.c b/networking/route.c index 881332b9f..b5490ddd8 100644 --- a/networking/route.c +++ b/networking/route.c @@ -478,7 +478,7 @@ static void set_flags(char *flagstr, int flags) /* also used in netstat */ void bb_displayroutes(int noresolve, int netstatfmt) { - char devname[64], flags[16], sdest[16], sgw[16]; + char devname[64], flags[16], *sdest, *sgw; unsigned long d, g, m; int flgs, ref, use, metric, mtu, win, ir; struct sockaddr_in s_addr; @@ -520,15 +520,14 @@ void bb_displayroutes(int noresolve, int netstatfmt) memset(&s_addr, 0, sizeof(struct sockaddr_in)); s_addr.sin_family = AF_INET; s_addr.sin_addr.s_addr = d; - INET_rresolve(sdest, sizeof(sdest), &s_addr, - (noresolve | 0x8000), m); /* Default instead of *. */ - + sdest = INET_rresolve(&s_addr, (noresolve | 0x8000), m); /* 'default' instead of '*' */ s_addr.sin_addr.s_addr = g; - INET_rresolve(sgw, sizeof(sgw), &s_addr, - (noresolve | 0x4000), m); /* Host instead of net. */ - + sgw = INET_rresolve(&s_addr, (noresolve | 0x4000), m); /* Host instead of net */ mask.s_addr = m; - printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask), flags); + /* "%15.15s" truncates hostnames, do we really want that? */ + printf("%-15.15s %-15.15s %-16s%-6s", sdest, sgw, inet_ntoa(mask), flags); + free(sdest); + free(sgw); if (netstatfmt) { printf("%5d %-5d %6d %s\n", mtu, win, ir, devname); } else { @@ -541,7 +540,7 @@ void bb_displayroutes(int noresolve, int netstatfmt) static void INET6_displayroutes(int noresolve) { - char addr6[128], naddr6[128]; + char addr6[128], *naddr6; /* In addr6x, we store both 40-byte ':'-delimited ipv6 addresses. * We read the non-delimited strings into the tail of the buffer * using fscanf and then modify the buffer by shifting forward @@ -581,7 +580,7 @@ static void INET6_displayroutes(int noresolve) do { if (!*p) { - if (i==40) { /* nul terminator for 1st address? */ + if (i == 40) { /* nul terminator for 1st address? */ addr6x[39] = 0; /* Fixup... need 0 instead of ':'. */ ++p; /* Skip and continue. */ continue; @@ -606,18 +605,19 @@ static void INET6_displayroutes(int noresolve) inet_pton(AF_INET6, addr6x + r, (struct sockaddr *) &snaddr6.sin6_addr); snaddr6.sin6_family = AF_INET6; - INET6_rresolve(naddr6, sizeof(naddr6), - (struct sockaddr_in6 *) &snaddr6, + naddr6 = INET6_rresolve((struct sockaddr_in6 *) &snaddr6, 0x0fff /* Apparently, upstream never resolves. */ ); if (!r) { /* 1st pass */ snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len); r += 40; + free(naddr6); } else { /* 2nd pass */ /* Print the info. */ printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n", - addr6, naddr6, flags, metric, refcnt, use, iface); + addr6, naddr6, flags, metric, refcnt, use, iface); + free(naddr6); break; } } while (1); diff --git a/networking/traceroute.c b/networking/traceroute.c index 9fc15d390..577738a97 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -802,22 +802,20 @@ packet_ok(unsigned char *buf, int cc, struct sockaddr_in *from, int seq) * numeric value, otherwise try for symbolic name. */ static inline void -inetname(struct sockaddr_in *from) +print_inetname(struct sockaddr_in *from) { - const char *n = NULL; const char *ina; - char name[257]; - if (!nflag && from->sin_addr.s_addr != INADDR_ANY) { - if (INET_rresolve(name, sizeof(name), from, 0x4000, - 0xffffffff) >= 0) - n = name; - } ina = inet_ntoa(from->sin_addr); if (nflag) printf(" %s", ina); - else + else { + char *n = NULL; + if (from->sin_addr.s_addr != INADDR_ANY) + n = xmalloc_sockaddr2host_noport((struct sockaddr*)from, sizeof(*from)); printf(" %s (%s)", (n ? n : ina), ina); + free(n); + } } static inline void @@ -830,7 +828,7 @@ print(unsigned char *buf, int cc, struct sockaddr_in *from) hlen = ip->ip_hl << 2; cc -= hlen; - inetname(from); + print_inetname(from); #if ENABLE_FEATURE_TRACEROUTE_VERBOSE if (verbose) printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst)); |