libc/netdb: Make host and serv optional for getnameinfo

and fix other minor style

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I107ee5b141f39e1e08967373b1ead4495bc55aa7
This commit is contained in:
Xiang Xiao 2020-03-30 01:22:29 +08:00 committed by patacongo
parent a709c83b2e
commit dd0aca6822
3 changed files with 58 additions and 64 deletions

View File

@ -48,8 +48,8 @@
struct errno_strmap_s
{
uint8_t errnum;
const char *str;
uint8_t errnum;
FAR const char *str;
};
/****************************************************************************

View File

@ -68,7 +68,7 @@ struct ai_s
FAR static struct ai_s *alloc_ai(int family, int socktype, int protocol,
int port, FAR void *addr)
{
struct ai_s *ai;
FAR struct ai_s *ai;
socklen_t addrlen;
addrlen = (family == AF_INET) ? sizeof(struct sockaddr_in)
@ -80,11 +80,11 @@ FAR static struct ai_s *alloc_ai(int family, int socktype, int protocol,
return ai;
}
ai->ai.ai_addr = (struct sockaddr *)&ai->sa;
ai->ai.ai_addrlen = addrlen;
ai->ai.ai_addr->sa_family = ai->ai.ai_family = family;
ai->ai.ai_socktype = socktype;
ai->ai.ai_protocol = protocol;
ai->ai.ai_addr = (FAR struct sockaddr *)&ai->sa;
ai->ai.ai_addrlen = addrlen;
ai->ai.ai_family = family;
ai->ai.ai_socktype = socktype;
ai->ai.ai_protocol = protocol;
switch (family)
{
@ -123,9 +123,9 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
int flags = 0;
int proto = 0;
int socktype = 0;
struct hostent *hp;
struct ai_s *ai;
struct ai_s *prev_ai = NULL;
FAR struct hostent *hp;
FAR struct ai_s *ai;
FAR struct ai_s *prev_ai = NULL;
const int valid_flags = AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST |
AI_NUMERICSERV | AI_V4MAPPED | AI_ALL |
AI_ADDRCONFIG;
@ -158,15 +158,15 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
if (servname != NULL)
{
char *endp;
struct servent *sp;
FAR char *endp;
FAR struct servent *sp;
port = strtol(servname, &endp, 10);
if (port > 0 && port <= 65535 && *endp == '\0')
{
/* Force network byte order */
port = HTONS(port);
port = htons(port);
}
else if ((flags & AI_NUMERICSERV) != 0)
{
@ -174,7 +174,7 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
}
else if ((sp = getservbyname(servname, NULL)) != NULL)
{
/* The sp_port field of struct servent is required to
/* The s_port field of struct servent is required to
* be in network byte order (per OpenGroup.org)
*/
@ -199,39 +199,35 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
#ifdef CONFIG_NET_IPv4
if (family == AF_INET || family == AF_UNSPEC)
{
ai = alloc_ai(AF_INET, socktype, proto, port, (void *)&addr);
if (ai == NULL)
ai = alloc_ai(AF_INET, socktype, proto, port, (FAR void *)&addr);
if (ai != NULL)
{
return EAI_MEMORY;
*res = (FAR struct addrinfo *)ai;
}
*res = (struct addrinfo *)ai;
}
#endif
#ifdef CONFIG_NET_IPv6
if (family == AF_INET6 || family == AF_UNSPEC)
{
ai = alloc_ai(AF_INET6, socktype, proto, port, (void *)&addr);
if (ai == NULL)
ai = alloc_ai(AF_INET6, socktype, proto, port, (FAR void *)&addr);
if (ai != NULL)
{
return (*res != NULL) ? OK : EAI_MEMORY;
}
/* Can return both IPv4 and IPv6 loopback. */
/* Can return both IPv4 and IPv6 loopback. */
if (*res != NULL)
{
(*res)->ai_next = (struct addrinfo *)ai;
}
else
{
*res = (struct addrinfo *)ai;
if (*res != NULL)
{
(*res)->ai_next = (FAR struct addrinfo *)ai;
}
else
{
*res = (FAR struct addrinfo *)ai;
}
}
}
#endif
return (*res != NULL) ? OK : EAI_FAMILY;
return (*res != NULL) ? OK : EAI_MEMORY;
}
if (hostname == NULL)
@ -244,12 +240,10 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
{
ai = alloc_ai(AF_INET, socktype, proto, port,
(FAR void *)&g_lo_ipv4addr);
if (ai == NULL)
if (ai != NULL)
{
return EAI_MEMORY;
*res = (FAR struct addrinfo *)ai;
}
*res = (struct addrinfo *)ai;
}
#endif
@ -258,25 +252,23 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
{
ai = alloc_ai(AF_INET6, socktype, proto, port,
(FAR void *)&g_lo_ipv6addr);
if (ai == NULL)
if (ai != NULL)
{
return (*res != NULL) ? OK : EAI_MEMORY;
}
/* Can return both IPv4 and IPv6 loopback. */
/* Can return both IPv4 and IPv6 loopback. */
if (*res != NULL)
{
(*res)->ai_next = (struct addrinfo *)ai;
}
else
{
*res = (struct addrinfo *)ai;
if (*res != NULL)
{
(*res)->ai_next = (FAR struct addrinfo *)ai;
}
else
{
*res = (FAR struct addrinfo *)ai;
}
}
}
#endif
return (*res != NULL) ? OK : EAI_FAMILY;
return (*res != NULL) ? OK : EAI_MEMORY;
#else
/* Local service, but no loopback so cannot succeed. */
@ -323,7 +315,7 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
* with the same contents."
*/
ai->ai.ai_canonname = (char *)hostname;
ai->ai.ai_canonname = (FAR char *)hostname;
/* Add result to linked list.
* TODO: RFC 3484/6724 destination address sort not implemented.
@ -331,11 +323,11 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
if (prev_ai != NULL)
{
prev_ai->ai.ai_next = (struct addrinfo *)ai;
prev_ai->ai.ai_next = (FAR struct addrinfo *)ai;
}
else
{
*res = (struct addrinfo *)ai;
*res = (FAR struct addrinfo *)ai;
}
prev_ai = ai;
@ -344,5 +336,5 @@ int getaddrinfo(FAR const char *hostname, FAR const char *servname,
return OK;
}
return EAI_AGAIN;
return h_errno;
}

View File

@ -68,21 +68,23 @@ int getnameinfo(FAR const struct sockaddr *addr, socklen_t addrlen,
int port;
int ret;
if (addr->sa_family == AF_INET &&
if (addr && addr->sa_family == AF_INET &&
addrlen == sizeof(struct sockaddr_in))
{
const struct sockaddr_in *sa_in = (const struct sockaddr_in *)addr;
FAR const struct sockaddr_in *sa_in;
sa_in = (FAR const struct sockaddr_in *)addr;
port = ntohs(sa_in->sin_port);
saddr = &sa_in->sin_addr;
saddr_len = sizeof(sa_in->sin_addr);
}
#ifdef CONFIG_NET_IPv6
else if (addr->sa_family == AF_INET6 &&
else if (addr && addr->sa_family == AF_INET6 &&
addrlen == sizeof(struct sockaddr_in6))
{
const struct sockaddr_in6 *sa_in6 = (const struct sockaddr_in6 *)addr;
FAR const struct sockaddr_in6 *sa_in6;
sa_in6 = (FAR const struct sockaddr_in6 *)addr;
port = ntohs(sa_in6->sin6_port);
saddr = &sa_in6->sin6_addr;
saddr_len = sizeof(sa_in6->sin6_addr);
@ -93,7 +95,7 @@ int getnameinfo(FAR const struct sockaddr *addr, socklen_t addrlen,
return EAI_FAMILY;
}
if (!(flags & NI_NUMERICHOST))
if (host && !(flags & NI_NUMERICHOST))
{
struct hostent hostent;
int h_errno;
@ -157,7 +159,7 @@ int getnameinfo(FAR const struct sockaddr *addr, socklen_t addrlen,
}
}
if (flags & NI_NUMERICHOST)
if (host && (flags & NI_NUMERICHOST))
{
if (!inet_ntop(addr->sa_family, saddr, host, hostlen))
{
@ -172,10 +174,10 @@ int getnameinfo(FAR const struct sockaddr *addr, socklen_t addrlen,
}
}
if (!(flags & NI_NUMERICSERV))
if (serv && !(flags & NI_NUMERICSERV))
{
struct servent servent;
struct servent *result;
FAR struct servent *result;
ret = getservbyport_r(port, flags & NI_DGRAM ? "udp" : "tcp",
&servent, serv, servlen, &result);
@ -202,7 +204,7 @@ int getnameinfo(FAR const struct sockaddr *addr, socklen_t addrlen,
}
}
if (flags & NI_NUMERICSERV)
if (serv && (flags & NI_NUMERICSERV))
{
snprintf(serv, servlen, "%d", port);
}