Skip to content

Commit

Permalink
LibC: Only accept the first response from LookupServer in netdb code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awesomekling committed Jan 26, 2020
1 parent 8fb9dc7 commit f24173b
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Libraries/LibC/netdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f24173b

Please sign in to comment.