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
+45 -53
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;
}