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
This commit is contained in:
Denis Vlasenko 2007-06-19 11:12:46 +00:00
parent 1b16bdaebf
commit 6d9ea24611
6 changed files with 111 additions and 135 deletions

View File

@ -18,9 +18,9 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst);
* & 0x4000: host instead of net, * & 0x4000: host instead of net,
* & 0x0fff: don't resolve * & 0x0fff: don't resolve
*/ */
int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
int numeric, unsigned int netmask);
int INET6_resolve(const char *name, struct sockaddr_in6 *sin6); int INET6_resolve(const char *name, struct sockaddr_in6 *sin6);
int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, int numeric);
/* These return malloced string */
char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask);
char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric);

View File

@ -72,26 +72,24 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
return 0; return 0;
} }
/* cache */
struct addr {
struct sockaddr_in addr;
char *name;
int host;
struct addr *next;
};
static struct addr *INET_nn = NULL; /* addr-to-name cache */
/* numeric: & 0x8000: default instead of *, /* numeric: & 0x8000: default instead of *,
* & 0x4000: host instead of net, * & 0x4000: host instead of net,
* & 0x0fff: don't resolve * & 0x0fff: don't resolve
*/ */
int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in, char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
int numeric, unsigned int netmask)
{ {
struct hostent *ent; /* addr-to-name cache */
struct netent *np; struct addr {
struct addr *next;
struct sockaddr_in addr;
int host;
char name[1];
};
static struct addr *cache = NULL;
struct addr *pn; struct addr *pn;
char *name;
uint32_t ad, host_ad; uint32_t ad, host_ad;
int host = 0; int host = 0;
@ -102,7 +100,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
s_in->sin_family); s_in->sin_family);
#endif #endif
errno = EAFNOSUPPORT; errno = EAFNOSUPPORT;
return -1; return NULL;
} }
ad = s_in->sin_addr.s_addr; ad = s_in->sin_addr.s_addr;
#ifdef DEBUG #ifdef DEBUG
@ -111,62 +109,58 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
if (ad == INADDR_ANY) { if (ad == INADDR_ANY) {
if ((numeric & 0x0FFF) == 0) { if ((numeric & 0x0FFF) == 0) {
if (numeric & 0x8000) if (numeric & 0x8000)
safe_strncpy(name, bb_str_default, len); return xstrdup(bb_str_default);
else return xstrdup("*");
safe_strncpy(name, "*", len);
return 0;
} }
} }
if (numeric & 0x0FFF) { if (numeric & 0x0FFF)
safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); return xstrdup(inet_ntoa(s_in->sin_addr));
return 0;
}
if ((ad & (~netmask)) != 0 || (numeric & 0x4000)) if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
host = 1; host = 1;
pn = INET_nn; pn = cache;
while (pn != NULL) { while (pn) {
if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
safe_strncpy(name, pn->name, len);
#ifdef DEBUG #ifdef DEBUG
bb_error_msg("rresolve: found %s %08x in cache", bb_error_msg("rresolve: found %s %08x in cache",
(host ? "host" : "net"), (unsigned)ad); (host ? "host" : "net"), (unsigned)ad);
#endif #endif
return 0; return xstrdup(pn->name);
} }
pn = pn->next; pn = pn->next;
} }
host_ad = ntohl(ad); host_ad = ntohl(ad);
np = NULL; name = NULL;
ent = NULL;
if (host) { if (host) {
struct hostent *ent;
#ifdef DEBUG #ifdef DEBUG
bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad); bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
#endif #endif
ent = gethostbyaddr((char *) &ad, 4, AF_INET); ent = gethostbyaddr((char *) &ad, 4, AF_INET);
if (ent != NULL) { if (ent)
safe_strncpy(name, ent->h_name, len); name = xstrdup(ent->h_name);
}
} else { } else {
/* Hmmm... this is very rare to have named nets,
* and this getnetbyaddr() call is the only one in bbox.
* Maybe get rid of or make configurable? */
struct netent *np;
#ifdef DEBUG #ifdef DEBUG
bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad); bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
#endif #endif
np = getnetbyaddr(host_ad, AF_INET); np = getnetbyaddr(host_ad, AF_INET);
if (np != NULL) { if (np)
safe_strncpy(name, np->n_name, len); name = xstrdup(np->n_name);
} }
} if (!name)
if (!ent && !np) { name = xstrdup(inet_ntoa(s_in->sin_addr));
safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
} pn->next = cache;
pn = xmalloc(sizeof(struct addr));
pn->addr = *s_in; pn->addr = *s_in;
pn->next = INET_nn;
pn->host = host; pn->host = host;
pn->name = xstrdup(name); strcpy(pn->name, name);
INET_nn = pn; cache = pn;
return 0; return name;
} }
#ifdef CONFIG_FEATURE_IPV6 #ifdef CONFIG_FEATURE_IPV6
@ -195,9 +189,9 @@ int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
#endif #endif
int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
int numeric)
{ {
char name[128];
int s; int s;
/* Grmpf. -FvK */ /* Grmpf. -FvK */
@ -207,29 +201,25 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
sin6->sin6_family); sin6->sin6_family);
#endif #endif
errno = EAFNOSUPPORT; errno = EAFNOSUPPORT;
return -1; return NULL;
} }
if (numeric & 0x7FFF) { if (numeric & 0x7FFF) {
inet_ntop(AF_INET6, &sin6->sin6_addr, name, len); inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
return 0; return xstrdup(name);
} }
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
if (numeric & 0x8000) { if (numeric & 0x8000)
strcpy(name, bb_str_default); return xstrdup(bb_str_default);
} else { return xstrdup("*");
name[0] = '*';
name[1] = '\0';
}
return 0;
} }
s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
name, len, NULL, 0, 0); name, sizeof(name), NULL, 0, 0);
if (s) { if (s) {
bb_error_msg("getnameinfo failed"); bb_error_msg("getnameinfo failed");
return -1; return NULL;
} }
return 0; return xstrdup(name);
} }
#endif /* CONFIG_FEATURE_IPV6 */ #endif /* CONFIG_FEATURE_IPV6 */

View File

@ -84,13 +84,12 @@ struct in6_ifreq {
/* Display an Internet socket address. */ /* Display an Internet socket address. */
static const char *INET_sprint(struct sockaddr *sap, int numeric) 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) if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
return "[NONE SET]"; return "[NONE SET]";
if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, buff = INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00);
numeric, 0xffffff00) != 0)
return NULL;
return buff; return buff;
} }
@ -167,12 +166,12 @@ static const struct aftype inet_aftype = {
/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */ /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
static const char *INET6_sprint(struct sockaddr *sap, int numeric) 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) if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
return "[NONE SET]"; return "[NONE SET]";
if (INET6_rresolve(buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric)) buff = INET6_rresolve((struct sockaddr_in6 *) sap, numeric);
return "[UNKNOWN]";
return buff; return buff;
} }

View File

@ -120,50 +120,33 @@ static void build_ipv4_addr(char* local_addr, struct sockaddr_in* localaddr)
((struct sockaddr *) localaddr)->sa_family = AF_INET; ((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!! */ if (!port)
const char *str = itoa(ntohs(port)); return "*";
if (!num) { if (!numeric) {
struct servent *se = getservbyport(port, proto); struct servent *se = getservbyport(port, proto);
if (se) if (se)
str = se->s_name; return se->s_name;
} }
if (!port) { /* hummm, we may return static buffer here!! */
str = "*"; return itoa(ntohs(port));
}
return str;
} }
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; enum { salen = USE_FEATURE_IPV6(sizeof(struct sockaddr_in6)) SKIP_FEATURE_IPV6(sizeof(struct sockaddr_in)) };
int max_len; char *host, *host_port;
int port_name_len;
// TODO: replace by xmalloc_sockaddr2host? /* Code which used "*" for INADDR_ANY is removed: it's ambiguous in IPv6,
#if ENABLE_FEATURE_IPV6 * while "0.0.0.0" is not. */
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) host = numeric ? xmalloc_sockaddr2dotted_noport(addr, salen)
? (PRINT_IP_MAX_SIZE_WIDE - 1) : xmalloc_sockaddr2host_noport(addr, salen);
: (PRINT_IP_MAX_SIZE - 1);
port_name_len = strlen(port_name); host_port = xasprintf("%s:%s", host, get_sname(htons(port), proto, numeric));
if ((strlen(ip_port) + port_name_len) > max_len) free(host);
ip_port[max_len - port_name_len] = '\0'; return host_port;
ip_port += strlen(ip_port);
*ip_port++ = ':';
strcpy(ip_port, port_name);
} }
static void tcp_do_one(int lnr, const char *line) 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)) if ((rem_port && (flags & NETSTAT_CONNECTED))
|| (!rem_port && (flags & NETSTAT_LISTENING)) || (!rem_port && (flags & NETSTAT_LISTENING))
) { ) {
snprint_ip_port(local_addr, sizeof(local_addr), char *l = ip_port_str(
(struct sockaddr *) &localaddr, local_port, (struct sockaddr *) &localaddr, local_port,
"tcp", flags & NETSTAT_NUMERIC); "tcp", flags & NETSTAT_NUMERIC);
snprint_ip_port(rem_addr, sizeof(rem_addr), char *r = ip_port_str(
(struct sockaddr *) &remaddr, rem_port, (struct sockaddr *) &remaddr, rem_port,
"tcp", flags & NETSTAT_NUMERIC); "tcp", flags & NETSTAT_NUMERIC);
printf(net_conn_line, 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)) if ((have_remaddr && (flags & NETSTAT_CONNECTED))
|| (!have_remaddr && (flags & NETSTAT_LISTENING)) || (!have_remaddr && (flags & NETSTAT_LISTENING))
) { ) {
snprint_ip_port(local_addr, sizeof(local_addr), char *l = ip_port_str(
(struct sockaddr *) &localaddr, local_port, (struct sockaddr *) &localaddr, local_port,
"udp", flags & NETSTAT_NUMERIC); "udp", flags & NETSTAT_NUMERIC);
snprint_ip_port(rem_addr, sizeof(rem_addr), char *r = ip_port_str(
(struct sockaddr *) &remaddr, rem_port, (struct sockaddr *) &remaddr, rem_port,
"udp", flags & NETSTAT_NUMERIC); "udp", flags & NETSTAT_NUMERIC);
printf(net_conn_line, 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)) if ((have_remaddr && (flags & NETSTAT_CONNECTED))
|| (!have_remaddr && (flags & NETSTAT_LISTENING)) || (!have_remaddr && (flags & NETSTAT_LISTENING))
) { ) {
snprint_ip_port(local_addr, sizeof(local_addr), char *l = ip_port_str(
(struct sockaddr *) &localaddr, local_port, (struct sockaddr *) &localaddr, local_port,
"raw", flags & NETSTAT_NUMERIC); "raw", flags & NETSTAT_NUMERIC);
snprint_ip_port(rem_addr, sizeof(rem_addr), char *r = ip_port_str(
(struct sockaddr *) &remaddr, rem_port, (struct sockaddr *) &remaddr, rem_port,
"raw", flags & NETSTAT_NUMERIC); "raw", flags & NETSTAT_NUMERIC);
printf(net_conn_line, printf(net_conn_line,
"raw", rxq, txq, local_addr, rem_addr, itoa(state)); "raw", rxq, txq, l, r, itoa(state));
free(l);
free(r);
} }
} }
} }

View File

@ -478,7 +478,7 @@ static void set_flags(char *flagstr, int flags)
/* also used in netstat */ /* also used in netstat */
void bb_displayroutes(int noresolve, int netstatfmt) 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; unsigned long d, g, m;
int flgs, ref, use, metric, mtu, win, ir; int flgs, ref, use, metric, mtu, win, ir;
struct sockaddr_in s_addr; struct sockaddr_in s_addr;
@ -520,15 +520,14 @@ void bb_displayroutes(int noresolve, int netstatfmt)
memset(&s_addr, 0, sizeof(struct sockaddr_in)); memset(&s_addr, 0, sizeof(struct sockaddr_in));
s_addr.sin_family = AF_INET; s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = d; s_addr.sin_addr.s_addr = d;
INET_rresolve(sdest, sizeof(sdest), &s_addr, sdest = INET_rresolve(&s_addr, (noresolve | 0x8000), m); /* 'default' instead of '*' */
(noresolve | 0x8000), m); /* Default instead of *. */
s_addr.sin_addr.s_addr = g; s_addr.sin_addr.s_addr = g;
INET_rresolve(sgw, sizeof(sgw), &s_addr, sgw = INET_rresolve(&s_addr, (noresolve | 0x4000), m); /* Host instead of net */
(noresolve | 0x4000), m); /* Host instead of net. */
mask.s_addr = m; 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) { if (netstatfmt) {
printf("%5d %-5d %6d %s\n", mtu, win, ir, devname); printf("%5d %-5d %6d %s\n", mtu, win, ir, devname);
} else { } else {
@ -541,7 +540,7 @@ void bb_displayroutes(int noresolve, int netstatfmt)
static void INET6_displayroutes(int noresolve) 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. /* In addr6x, we store both 40-byte ':'-delimited ipv6 addresses.
* We read the non-delimited strings into the tail of the buffer * We read the non-delimited strings into the tail of the buffer
* using fscanf and then modify the buffer by shifting forward * using fscanf and then modify the buffer by shifting forward
@ -581,7 +580,7 @@ static void INET6_displayroutes(int noresolve)
do { do {
if (!*p) { 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 ':'. */ addr6x[39] = 0; /* Fixup... need 0 instead of ':'. */
++p; /* Skip and continue. */ ++p; /* Skip and continue. */
continue; continue;
@ -606,18 +605,19 @@ static void INET6_displayroutes(int noresolve)
inet_pton(AF_INET6, addr6x + r, inet_pton(AF_INET6, addr6x + r,
(struct sockaddr *) &snaddr6.sin6_addr); (struct sockaddr *) &snaddr6.sin6_addr);
snaddr6.sin6_family = AF_INET6; snaddr6.sin6_family = AF_INET6;
INET6_rresolve(naddr6, sizeof(naddr6), naddr6 = INET6_rresolve((struct sockaddr_in6 *) &snaddr6,
(struct sockaddr_in6 *) &snaddr6,
0x0fff /* Apparently, upstream never resolves. */ 0x0fff /* Apparently, upstream never resolves. */
); );
if (!r) { /* 1st pass */ if (!r) { /* 1st pass */
snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len); snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len);
r += 40; r += 40;
free(naddr6);
} else { /* 2nd pass */ } else { /* 2nd pass */
/* Print the info. */ /* Print the info. */
printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n", 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; break;
} }
} while (1); } while (1);

View File

@ -802,22 +802,20 @@ packet_ok(unsigned char *buf, int cc, struct sockaddr_in *from, int seq)
* numeric value, otherwise try for symbolic name. * numeric value, otherwise try for symbolic name.
*/ */
static inline void static inline void
inetname(struct sockaddr_in *from) print_inetname(struct sockaddr_in *from)
{ {
const char *n = NULL;
const char *ina; 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); ina = inet_ntoa(from->sin_addr);
if (nflag) if (nflag)
printf(" %s", ina); 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); printf(" %s (%s)", (n ? n : ina), ina);
free(n);
}
} }
static inline void static inline void
@ -830,7 +828,7 @@ print(unsigned char *buf, int cc, struct sockaddr_in *from)
hlen = ip->ip_hl << 2; hlen = ip->ip_hl << 2;
cc -= hlen; cc -= hlen;
inetname(from); print_inetname(from);
#if ENABLE_FEATURE_TRACEROUTE_VERBOSE #if ENABLE_FEATURE_TRACEROUTE_VERBOSE
if (verbose) if (verbose)
printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst)); printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst));