summaryrefslogtreecommitdiff
path: root/libc/inet
diff options
context:
space:
mode:
Diffstat (limited to 'libc/inet')
-rw-r--r--libc/inet/getnet.c25
-rw-r--r--libc/inet/getproto.c25
-rw-r--r--libc/inet/getservice.c43
-rw-r--r--libc/inet/resolv.c20
4 files changed, 46 insertions, 67 deletions
diff --git a/libc/inet/getnet.c b/libc/inet/getnet.c
index c604b63d3..9049f97af 100644
--- a/libc/inet/getnet.c
+++ b/libc/inet/getnet.c
@@ -27,9 +27,11 @@ aliases: case sensitive optional space or tab separated list of other names
#include <bits/uClibc_mutex.h>
__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
-#define MAXALIASES 35
-#define BUFSZ (80) /* one line */
-#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXALIASES))
+#define MINTOKENS 2
+#define MAXALIASES 8
+#define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
+#define BUFSZ (255) /* one line */
+#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
static parser_t *netp = NULL;
static struct netent nete;
@@ -65,10 +67,8 @@ int getnetent_r(struct netent *result_buf,
int *h_errnop
)
{
- char **alias, *cp = NULL;
- char **net_aliases;
char **tok = NULL;
- const size_t aliaslen = sizeof(*net_aliases) * MAXALIASES;
+ const size_t aliaslen = sizeof(char *) * MAXTOKENS;
int ret = ERANGE;
*result = NULL;
@@ -86,7 +86,7 @@ int getnetent_r(struct netent *result_buf,
netp->data_len = aliaslen;
netp->line_len = buflen - aliaslen;
/* <name>[[:space:]]<netnumber>[[:space:]][<aliases>] */
- if (!config_read(netp, &tok, 3, 2, "# \t/", PARSE_NORMAL)) {
+ if (!config_read(netp, &tok, MAXTOKENS-1, MINTOKENS, "# \t/", PARSE_NORMAL)) {
goto DONE;
}
result_buf->n_name = *(tok++);
@@ -110,16 +110,7 @@ int getnetent_r(struct netent *result_buf,
sa4_to_uint32(addri->ai_addr);
freeaddrinfo(addri);
}
- result_buf->n_aliases = alias = net_aliases = tok;
- cp = *alias;
- while (cp && *cp) {
- if (alias < &net_aliases[MAXALIASES - 1])
- *alias++ = cp;
- cp = strpbrk(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
- }
- *alias = NULL;
+ result_buf->n_aliases = tok;
*result = result_buf;
ret = 0;
DONE:
diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c
index bcf507bda..c59da7e66 100644
--- a/libc/inet/getproto.c
+++ b/libc/inet/getproto.c
@@ -27,9 +27,11 @@ aliases: case sensitive optional space or tab separated list of other names
#include <bits/uClibc_mutex.h>
__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
-#define MAXALIASES 35
-#define BUFSZ (80) /* one line */
-#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXALIASES))
+#define MINTOKENS 2
+#define MAXALIASES 8 /* will probably never be more than one */
+#define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
+#define BUFSZ (255) /* one line */
+#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
static parser_t *protop = NULL;
static struct protoent protoe;
@@ -63,10 +65,8 @@ libc_hidden_def(endprotoent)
int getprotoent_r(struct protoent *result_buf,
char *buf, size_t buflen, struct protoent **result)
{
- char **alias, *cp = NULL;
- char **proto_aliases;
char **tok = NULL;
- const size_t aliaslen = sizeof(*proto_aliases) * MAXALIASES;
+ const size_t aliaslen = sizeof(char *) * MAXTOKENS;
int ret = ERANGE;
*result = NULL;
@@ -85,21 +85,12 @@ int getprotoent_r(struct protoent *result_buf,
protop->data_len = aliaslen;
protop->line_len = buflen - aliaslen;
/* <name>[[:space:]]<protonumber>[[:space:]][<aliases>] */
- if (!config_read(protop, &tok, 3, 2, "# \t/", PARSE_NORMAL)) {
+ if (!config_read(protop, &tok, MAXTOKENS - 1, MINTOKENS, "# \t/", PARSE_NORMAL)) {
goto DONE;
}
result_buf->p_name = *(tok++);
result_buf->p_proto = atoi(*(tok++));
- result_buf->p_aliases = alias = proto_aliases = tok;
- cp = *alias;
- while (cp && *cp) {
- if (alias < &proto_aliases[MAXALIASES - 1])
- *alias++ = cp;
- cp = strpbrk(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
- }
- *alias = NULL;
+ result_buf->p_aliases = tok;
*result = result_buf;
ret = 0;
DONE:
diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c
index 47d26a262..183099f5c 100644
--- a/libc/inet/getservice.c
+++ b/libc/inet/getservice.c
@@ -28,9 +28,11 @@ aliases: case sensitive optional space or tab separated list of other names
#include <bits/uClibc_mutex.h>
__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
-#define MAXALIASES 35
-#define BUFSZ (80) /* one line */
-#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXALIASES))
+#define MINTOKENS 3
+#define MAXALIASES 8 /* we seldomly need more than 1 alias */
+#define MAXTOKENS (MINTOKENS + MAXALIASES + 1)
+#define BUFSZ (255) /* one line */
+#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXTOKENS))
static parser_t *servp = NULL;
static struct servent serve;
@@ -65,11 +67,9 @@ libc_hidden_def(endservent)
int getservent_r(struct servent *result_buf,
char *buf, size_t buflen, struct servent **result)
{
- char **alias;
- char **serv_aliases;
char **tok = NULL;
- const size_t aliaslen = sizeof(*serv_aliases) * MAXALIASES;
- int ret = ENOENT;
+ const size_t aliaslen = sizeof(char *) * MAXTOKENS;
+ int ret = ERANGE;
*result = NULL;
if (buflen < aliaslen
@@ -77,7 +77,7 @@ int getservent_r(struct servent *result_buf,
goto DONE_NOUNLOCK;
__UCLIBC_MUTEX_LOCK(mylock);
-
+ ret = ENOENT;
if (servp == NULL)
setservent(serv_stayopen);
if (servp == NULL)
@@ -87,14 +87,13 @@ int getservent_r(struct servent *result_buf,
servp->data_len = aliaslen;
servp->line_len = buflen - aliaslen;
/* <name>[[:space:]]<port>/<proto>[[:space:]][<aliases>] */
- if (!config_read(servp, &tok, MAXALIASES, 3, "# \t/", PARSE_NORMAL)) {
- ret = ERANGE;
+ if (!config_read(servp, &tok, MAXTOKENS - 1, MINTOKENS, "# \t/", PARSE_NORMAL)) {
goto DONE;
}
result_buf->s_name = *(tok++);
result_buf->s_port = htons((u_short) atoi(*(tok++)));
result_buf->s_proto = *(tok++);
- result_buf->s_aliases = alias = serv_aliases = tok;
+ result_buf->s_aliases = tok;
*result = result_buf;
ret = 0;
DONE:
@@ -107,9 +106,8 @@ libc_hidden_def(getservent_r)
static void __initbuf(void)
{
- if (servbuf)
- servbuf_sz += BUFSZ;
- servbuf = realloc(servbuf, servbuf_sz);
+ if (!servbuf)
+ servbuf = malloc(SBUFSIZE);
if (!servbuf)
abort();
}
@@ -118,9 +116,8 @@ struct servent *getservent(void)
{
struct servent *result;
- do {
- __initbuf();
- } while (getservent_r(&serve, servbuf, servbuf_sz, &result) == ERANGE);
+ __initbuf();
+ getservent_r(&serve, servbuf, servbuf_sz, &result);
return result;
}
@@ -155,10 +152,8 @@ struct servent *getservbyname(const char *name, const char *proto)
{
struct servent *result;
- do {
- __initbuf();
- } while (getservbyname_r(name, proto, &serve, servbuf, servbuf_sz, &result)
- == ERANGE);
+ __initbuf();
+ getservbyname_r(name, proto, &serve, servbuf, servbuf_sz, &result);
return result;
}
@@ -188,10 +183,8 @@ struct servent *getservbyport(int port, const char *proto)
{
struct servent *result;
- do {
- __initbuf();
- } while (getservbyport_r(port, proto, &serve, servbuf, servbuf_sz, &result)
- == ERANGE);
+ __initbuf();
+ getservbyport_r(port, proto, &serve, servbuf, servbuf_sz, &result);
return result;
}
libc_hidden_def(getservbyport)
diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c
index 364a4b9f5..47bab7519 100644
--- a/libc/inet/resolv.c
+++ b/libc/inet/resolv.c
@@ -335,7 +335,7 @@ Domain name in a message can be represented as either:
#define MAX_RECURSE 5
-#define MAXALIASES (6)
+#define MAXALIASES (4)
#define BUFSZ (80) /* one line */
#define SBUFSIZE (BUFSZ + 1 + (sizeof(char *) * MAXALIASES))
@@ -1587,6 +1587,11 @@ parser_t * __open_etc_hosts(void)
return parser;
}
+#define MINTOKENS 2 //dotted ip address + canonical name
+#define MAXTOKENS (MINTOKENS + MAXALIASES)
+#define HALISTOFF (sizeof(char*) * MAXTOKENS)
+#define INADDROFF (HALISTOFF + 2 * sizeof(char*))
+
int attribute_hidden __read_etc_hosts_r(
parser_t * parser,
const char *name,
@@ -1601,8 +1606,7 @@ int attribute_hidden __read_etc_hosts_r(
char **host_aliases;
char **tok = NULL;
struct in_addr *h_addr0 = NULL;
-#define ALIASOFF (sizeof(*host_aliases) * MAXALIASES + 2 * sizeof(char*))
- const size_t aliaslen = ALIASOFF +
+ const size_t aliaslen = INADDROFF +
#ifdef __UCLIBC_HAS_IPV6__
sizeof(struct in6_addr)
#else
@@ -1622,8 +1626,8 @@ int attribute_hidden __read_etc_hosts_r(
return errno;
}
/* Layout in buf:
- * char **alias for MAXALIAS aliases
- * char **h_addr_list[1] = {*in[6]_addr, NULL}
+ * char *alias[MAXTOKENS] = {address, name, aliases...}
+ * char **h_addr_list[1] = {*in[6]_addr, NULL}
* struct in[6]_addr
* char line_buffer[BUFSZ+];
*/
@@ -1632,7 +1636,7 @@ int attribute_hidden __read_etc_hosts_r(
parser->line_len = buflen - aliaslen;
*h_errnop = HOST_NOT_FOUND;
/* <ip>[[:space:]][<aliases>] */
- while (config_read(parser, &tok, MAXALIASES, 2, "# \t", PARSE_NORMAL)) {
+ while (config_read(parser, &tok, MAXTOKENS, MINTOKENS, "# \t", PARSE_NORMAL)) {
result_buf->h_aliases = alias = host_aliases = tok+1;
if (action == GETHOSTENT) {
/* Return whatever the next entry happens to be. */
@@ -1650,9 +1654,9 @@ int attribute_hidden __read_etc_hosts_r(
}
found:
result_buf->h_name = *(result_buf->h_aliases++);
- result_buf->h_addr_list = (char**)(buf + ALIASOFF);
+ result_buf->h_addr_list = (char**)(buf + HALISTOFF);
*(result_buf->h_addr_list + 1) = '\0';
- h_addr0 = (struct in_addr*)(buf + ALIASOFF + 2 * sizeof (char*));
+ h_addr0 = (struct in_addr*)(buf + INADDROFF);
result_buf->h_addr = (char*)h_addr0;
if (0) /* nothing */;
#ifdef __UCLIBC_HAS_IPV4__