aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/inet_common.c104
1 files changed, 47 insertions, 57 deletions
diff --git a/libbb/inet_common.c b/libbb/inet_common.c
index 44b44a4cc..8449201ab 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -72,26 +72,24 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
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 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
s_in->sin_family);
#endif
errno = EAFNOSUPPORT;
- return -1;
+ return NULL;
}
ad = s_in->sin_addr.s_addr;
#ifdef DEBUG
@@ -111,62 +109,58 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
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 (!ent && !np) {
- safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
+ if (np)
+ name = xstrdup(np->n_name);
}
- 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 @@ int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
#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 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
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 */