LibC: Only accept the first response from LookupServer in netdb code

If a DNS server responds with multiple answers for a question, we will
get a newline-separated sequence of answers from LookupServer.

However, we don't handle this properly yet in LibC, so just split the
response by line and only care about the first answer for now.
This commit is contained in:
Andreas Kling 2020-01-26 12:23:37 +01:00
parent 8fb9dc7425
commit f24173b0f1
Notes: sideshowbarker 2024-07-19 09:48:50 +09:00

View File

@ -106,12 +106,17 @@ hostent* gethostbyname(const char* name)
perror("recv");
return nullptr;
}
buffer[nrecv] = '\0';
if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
return nullptr;
int rc = inet_pton(AF_INET, buffer, &__gethostbyname_address);
auto responses = String(buffer, nrecv).split('\n');
if (responses.is_empty())
return nullptr;
auto& response = responses[0];
int rc = inet_pton(AF_INET, response.characters(), &__gethostbyname_address);
if (rc <= 0)
return nullptr;
@ -169,16 +174,17 @@ hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
perror("recv");
return nullptr;
}
if (nrecv > 1) {
// Strip newline.
buffer[nrecv - 1] = '\0';
}
buffer[nrecv] = '\0';
if (!memcmp(buffer, "Not found.", sizeof("Not found.") - 1))
return nullptr;
strncpy(__gethostbyaddr_name_buffer, buffer, max(sizeof(__gethostbyaddr_name_buffer), (size_t)nrecv));
auto responses = String(buffer, nrecv).split('\n');
if (responses.is_empty())
return nullptr;
auto& response = responses[0];
strncpy(__gethostbyaddr_name_buffer, response.characters(), max(sizeof(__gethostbyaddr_name_buffer), response.length()));
__gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
__gethostbyaddr_buffer.h_aliases = nullptr;