Commit d87700c9 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

- Return errors in case of overflow

- Do not use non-thread-safe stuff
parent 419f5e33
...@@ -163,50 +163,27 @@ __getnameinfo( const struct sockaddr *sa, socklen_t salen, ...@@ -163,50 +163,27 @@ __getnameinfo( const struct sockaddr *sa, socklen_t salen,
if (host != NULL) if (host != NULL)
{ {
int solved = 0;
/* host name resolution */ /* host name resolution */
if (!(flags & NI_NUMERICHOST)) if (!(flags & NI_NUMERICHOST))
{ {
struct hostent *hent; if (flags & NI_NAMEREQD)
return EAI_NONAME;
hent = gethostbyaddr ((const void*)&addr->sin_addr,
4, AF_INET);
if (hent != NULL)
{
strlcpy (host, hent->h_name, hostlen);
/*
* only keep first part of hostname
* if user don't want fully qualified
* domain name
*/
if (flags & NI_NOFQDN)
{
char *ptr;
ptr = strchr (host, '.');
if (ptr != NULL)
*ptr = 0;
}
solved = 1;
}
else if (flags & NI_NAMEREQD)
return gai_error_from_herrno ();
} }
if (!solved) /* inet_ntoa() is not thread-safe, do not use it */
/* inet_ntoa() can't fail */ uint32_t ipv4 = ntohl (addr->sin_addr);
strlcpy (host, inet_ntoa (addr->sin_addr), hostlen);
if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
(ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
ipv4 & 0xff) >= hostlen)
return EAI_OVERFLOW;
} }
if (serv != NULL) if (serv != NULL)
{ {
snprintf (serv, servlen, "%u", if (snprintf (serv, servlen, "%u",
(unsigned int)ntohs (addr->sin_port)); (unsigned int)ntohs (addr->sin_port)) >= servlen)
serv[servlen - 1] = '\0'; return EAI_OVERFLOW;
} }
} }
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment