LibC: Let gethostbyname() handle IPv4 address as input.

It would be neat to do a reverse lookup too, but for now we just parse
the IPv4 address into a dword.
This commit is contained in:
Andreas Kling 2019-03-20 03:37:05 +01:00
parent f47945759b
commit 696f9c5bc0
Notes: sideshowbarker 2024-07-19 14:59:47 +09:00

View file

@ -17,6 +17,25 @@ static in_addr_t* __gethostbyname_address_list_buffer[2];
hostent* gethostbyname(const char* name)
{
unsigned a;
unsigned b;
unsigned c;
unsigned d;
int count = sscanf(name, "%u.%u.%u.%u", &a, &b, &c, &d);
if (count == 4 && a < 256 && b < 256 && c < 256 && d < 256) {
sprintf(__gethostbyname_name_buffer, "%u.%u.%u.%u", a, b, c, d);
__gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
__gethostbyname_buffer.h_aliases = nullptr;
__gethostbyname_buffer.h_addrtype = AF_INET;
new (&__gethostbyname_address) IPv4Address(a, b, c, d);
__gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
__gethostbyname_address_list_buffer[1] = nullptr;
__gethostbyname_buffer.h_addr_list = (char**)__gethostbyname_address_list_buffer;
__gethostbyname_buffer.h_length = 4;
return &__gethostbyname_buffer;
}
int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0) {
perror("socket");