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/libbb/inet_common.c b/libbb/inet_common.c
index 44b44a4..8449201 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -72,26 +72,24 @@
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 *,
* & 0x4000: host instead of net,
* & 0x0fff: don't resolve
*/
-int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
- int numeric, unsigned int netmask)
+char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask)
{
- struct hostent *ent;
- struct netent *np;
+ /* addr-to-name cache */
+ struct addr {
+ struct addr *next;
+ struct sockaddr_in addr;
+ int host;
+ char name[1];
+ };
+ static struct addr *cache = NULL;
+
struct addr *pn;
+ char *name;
uint32_t ad, host_ad;
int host = 0;
@@ -102,7 +100,7 @@
s_in->sin_family);
#endif
errno = EAFNOSUPPORT;
- return -1;
+ return NULL;
}
ad = s_in->sin_addr.s_addr;
#ifdef DEBUG
@@ -111,62 +109,58 @@
if (ad == INADDR_ANY) {
if ((numeric & 0x0FFF) == 0) {
if (numeric & 0x8000)
- safe_strncpy(name, bb_str_default, len);
- else
- safe_strncpy(name, "*", len);
- return 0;
+ return xstrdup(bb_str_default);
+ return xstrdup("*");
}
}
- if (numeric & 0x0FFF) {
- safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
- return 0;
- }
+ if (numeric & 0x0FFF)
+ return xstrdup(inet_ntoa(s_in->sin_addr));
if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
host = 1;
- pn = INET_nn;
- while (pn != NULL) {
+ pn = cache;
+ while (pn) {
if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
- safe_strncpy(name, pn->name, len);
#ifdef DEBUG
bb_error_msg("rresolve: found %s %08x in cache",
(host ? "host" : "net"), (unsigned)ad);
#endif
- return 0;
+ return xstrdup(pn->name);
}
pn = pn->next;
}
host_ad = ntohl(ad);
- np = NULL;
- ent = NULL;
+ name = NULL;
if (host) {
+ struct hostent *ent;
#ifdef DEBUG
bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
#endif
ent = gethostbyaddr((char *) &ad, 4, AF_INET);
- if (ent != NULL) {
- safe_strncpy(name, ent->h_name, len);
- }
+ if (ent)
+ name = xstrdup(ent->h_name);
} 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
bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
#endif
np = getnetbyaddr(host_ad, AF_INET);
- if (np != NULL) {
- safe_strncpy(name, np->n_name, len);
- }
+ if (np)
+ name = xstrdup(np->n_name);
}
- if (!ent && !np) {
- safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
- }
- pn = xmalloc(sizeof(struct addr));
+ if (!name)
+ name = xstrdup(inet_ntoa(s_in->sin_addr));
+ pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
+ pn->next = cache;
pn->addr = *s_in;
- pn->next = INET_nn;
pn->host = host;
- pn->name = xstrdup(name);
- INET_nn = pn;
- return 0;
+ strcpy(pn->name, name);
+ cache = pn;
+ return name;
}
#ifdef CONFIG_FEATURE_IPV6
@@ -195,9 +189,9 @@
#endif
-int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
- int numeric)
+char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
{
+ char name[128];
int s;
/* Grmpf. -FvK */
@@ -207,29 +201,25 @@
sin6->sin6_family);
#endif
errno = EAFNOSUPPORT;
- return -1;
+ return NULL;
}
if (numeric & 0x7FFF) {
- inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
- return 0;
+ inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name));
+ return xstrdup(name);
}
if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
- if (numeric & 0x8000) {
- strcpy(name, bb_str_default);
- } else {
- name[0] = '*';
- name[1] = '\0';
- }
- return 0;
+ if (numeric & 0x8000)
+ return xstrdup(bb_str_default);
+ return xstrdup("*");
}
s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
- name, len, NULL, 0, 0);
+ name, sizeof(name), NULL, 0, 0);
if (s) {
bb_error_msg("getnameinfo failed");
- return -1;
+ return NULL;
}
- return 0;
+ return xstrdup(name);
}
#endif /* CONFIG_FEATURE_IPV6 */