mirror of
https://github.com/apache/nuttx.git
synced 2026-05-27 11:26:12 +08:00
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:
@@ -54,11 +54,10 @@
|
|||||||
FAR struct servent *getservbyname(FAR const char *name, FAR const char *proto)
|
FAR struct servent *getservbyname(FAR const char *name, FAR const char *proto)
|
||||||
{
|
{
|
||||||
static struct servent ent;
|
static struct servent ent;
|
||||||
static char *buf[2];
|
|
||||||
struct servent *res;
|
struct servent *res;
|
||||||
int ret;
|
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;
|
return (ret != OK) ? NULL : res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,32 +78,16 @@ int getservbyname_r(FAR const char *name, FAR const char *proto,
|
|||||||
FAR struct servent *result_buf, FAR char *buf,
|
FAR struct servent *result_buf, FAR char *buf,
|
||||||
size_t buflen, FAR struct servent **result)
|
size_t buflen, FAR struct servent **result)
|
||||||
{
|
{
|
||||||
char *end = "";
|
|
||||||
int protocol;
|
int protocol;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
DEBUGASSERT(name != NULL && buf != NULL);
|
DEBUGASSERT(name != NULL);
|
||||||
DEBUGASSERT(result_buf != NULL && result != NULL);
|
DEBUGASSERT(result_buf != NULL && result != NULL);
|
||||||
|
|
||||||
/* Linux man page says result must be NULL in case of failure. */
|
/* Linux man page says result must be NULL in case of failure. */
|
||||||
|
|
||||||
*result = NULL;
|
*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)
|
if (proto == NULL)
|
||||||
{
|
{
|
||||||
protocol = 0;
|
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 &&
|
if (strcmp(name, g_services_db[i].s_name) == 0 &&
|
||||||
(protocol == 0 || protocol == g_services_db[i].s_protocol))
|
(protocol == 0 || protocol == g_services_db[i].s_protocol))
|
||||||
{
|
{
|
||||||
result_buf->s_name = (char *)name;
|
result_buf->s_name = (FAR char *)name;
|
||||||
result_buf->s_aliases = (void *)buf;
|
result_buf->s_aliases = NULL;
|
||||||
result_buf->s_aliases[0] = (char *)name;
|
result_buf->s_port = htons(g_services_db[i].s_port);
|
||||||
result_buf->s_aliases[1] = NULL;
|
|
||||||
result_buf->s_port = HTONS(g_services_db[i].s_port);
|
|
||||||
|
|
||||||
if (g_services_db[i].s_protocol == IPPROTO_TCP)
|
if (g_services_db[i].s_protocol == IPPROTO_TCP)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -58,11 +58,10 @@
|
|||||||
FAR struct servent *getservbyport(int port, FAR const char *proto)
|
FAR struct servent *getservbyport(int port, FAR const char *proto)
|
||||||
{
|
{
|
||||||
static struct servent ent;
|
static struct servent ent;
|
||||||
static FAR char *buf[2];
|
|
||||||
struct servent *res;
|
struct servent *res;
|
||||||
int ret;
|
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;
|
return (ret != OK) ? NULL : res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,20 +70,12 @@ int getservbyport_r(int port, FAR const char *proto,
|
|||||||
int protocol;
|
int protocol;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
DEBUGASSERT(buf != NULL);
|
|
||||||
DEBUGASSERT(result_buf != NULL && result != NULL);
|
DEBUGASSERT(result_buf != NULL && result != NULL);
|
||||||
|
|
||||||
/* Linux man page says result must be NULL in case of failure. */
|
/* Linux man page says result must be NULL in case of failure. */
|
||||||
|
|
||||||
*result = NULL;
|
*result = NULL;
|
||||||
|
|
||||||
/* We need space for two pointers for hostalias strings. */
|
|
||||||
|
|
||||||
if (buflen < 2 * sizeof(char *))
|
|
||||||
{
|
|
||||||
return ERANGE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (proto == NULL)
|
if (proto == NULL)
|
||||||
{
|
{
|
||||||
protocol = 0;
|
protocol = 0;
|
||||||
@@ -106,11 +98,9 @@ int getservbyport_r(int port, FAR const char *proto,
|
|||||||
if (port == g_services_db[i].s_port &&
|
if (port == g_services_db[i].s_port &&
|
||||||
(protocol == 0 || protocol == g_services_db[i].s_protocol))
|
(protocol == 0 || protocol == g_services_db[i].s_protocol))
|
||||||
{
|
{
|
||||||
result_buf->s_name = (char *)g_services_db[i].s_name;
|
result_buf->s_name = (FAR char *)g_services_db[i].s_name;
|
||||||
result_buf->s_aliases = (void *)buf;
|
result_buf->s_aliases = NULL;
|
||||||
result_buf->s_aliases[0] = (char *)g_services_db[i].s_name;
|
result_buf->s_port = htons(port);
|
||||||
result_buf->s_aliases[1] = NULL;
|
|
||||||
result_buf->s_port = HTONS(g_services_db[i].s_port);
|
|
||||||
|
|
||||||
if (g_services_db[i].s_protocol == IPPROTO_TCP)
|
if (g_services_db[i].s_protocol == IPPROTO_TCP)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user