libc/netdb: Return null alias for getservbyport_r/getservbyname_r

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I82728a8ea9a5136e0154cd67ec8de70a4e0ac6ed
This commit is contained in:
Xiang Xiao 2020-03-29 16:27:56 +08:00 committed by patacongo
parent d05de762bc
commit 9e277c6c50
4 changed files with 9 additions and 39 deletions

View File

@ -54,11 +54,10 @@
FAR struct servent *getservbyname(FAR const char *name, FAR const char *proto)
{
static struct servent ent;
static char *buf[2];
struct servent *res;
int ret;
ret = getservbyname_r(name, proto, &ent, (void *)buf, sizeof buf, &res);
ret = getservbyname_r(name, proto, &ent, NULL, 0, &res);
return (ret != OK) ? NULL : res;
}

View File

@ -78,32 +78,16 @@ int getservbyname_r(FAR const char *name, FAR const char *proto,
FAR struct servent *result_buf, FAR char *buf,
size_t buflen, FAR struct servent **result)
{
char *end = "";
int protocol;
int i;
DEBUGASSERT(name != NULL && buf != NULL);
DEBUGASSERT(name != NULL);
DEBUGASSERT(result_buf != NULL && result != NULL);
/* Linux man page says result must be NULL in case of failure. */
*result = NULL;
/* We need space for two pointers for hostalias strings. */
if (buflen < 2 * sizeof(char *))
{
return ERANGE;
}
/* Numeric port number strings are not service records. */
strtoul(name, &end, 10);
if (*end == '\0')
{
return ENOENT;
}
if (proto == NULL)
{
protocol = 0;
@ -126,11 +110,9 @@ int getservbyname_r(FAR const char *name, FAR const char *proto,
if (strcmp(name, g_services_db[i].s_name) == 0 &&
(protocol == 0 || protocol == g_services_db[i].s_protocol))
{
result_buf->s_name = (char *)name;
result_buf->s_aliases = (void *)buf;
result_buf->s_aliases[0] = (char *)name;
result_buf->s_aliases[1] = NULL;
result_buf->s_port = HTONS(g_services_db[i].s_port);
result_buf->s_name = (FAR char *)name;
result_buf->s_aliases = NULL;
result_buf->s_port = htons(g_services_db[i].s_port);
if (g_services_db[i].s_protocol == IPPROTO_TCP)
{

View File

@ -58,11 +58,10 @@
FAR struct servent *getservbyport(int port, FAR const char *proto)
{
static struct servent ent;
static FAR char *buf[2];
struct servent *res;
int ret;
ret = getservbyport_r(port, proto, &ent, (FAR void *)buf, sizeof buf, &res);
ret = getservbyport_r(port, proto, &ent, NULL, 0, &res);
return (ret != OK) ? NULL : res;
}

View File

@ -70,20 +70,12 @@ int getservbyport_r(int port, FAR const char *proto,
int protocol;
int i;
DEBUGASSERT(buf != NULL);
DEBUGASSERT(result_buf != NULL && result != NULL);
/* Linux man page says result must be NULL in case of failure. */
*result = NULL;
/* We need space for two pointers for hostalias strings. */
if (buflen < 2 * sizeof(char *))
{
return ERANGE;
}
if (proto == NULL)
{
protocol = 0;
@ -106,11 +98,9 @@ int getservbyport_r(int port, FAR const char *proto,
if (port == g_services_db[i].s_port &&
(protocol == 0 || protocol == g_services_db[i].s_protocol))
{
result_buf->s_name = (char *)g_services_db[i].s_name;
result_buf->s_aliases = (void *)buf;
result_buf->s_aliases[0] = (char *)g_services_db[i].s_name;
result_buf->s_aliases[1] = NULL;
result_buf->s_port = HTONS(g_services_db[i].s_port);
result_buf->s_name = (FAR char *)g_services_db[i].s_name;
result_buf->s_aliases = NULL;
result_buf->s_port = htons(port);
if (g_services_db[i].s_protocol == IPPROTO_TCP)
{