LibC: Implement inet_ntop for IPv4 addresses

This commit is contained in:
Bananymous 2024-02-08 11:59:11 +02:00
parent 49889858fa
commit 6fb69a1dc2
1 changed files with 23 additions and 0 deletions

View File

@ -50,3 +50,26 @@ char* inet_ntoa(struct in_addr in)
);
return buffer;
}
const char* inet_ntop(int af, const void* __restrict src, char* __restrict dst, socklen_t size)
{
if (af == AF_INET)
{
if (size < INET_ADDRSTRLEN)
{
errno = ENOSPC;
return nullptr;
}
uint32_t he = ntohl(reinterpret_cast<const in_addr*>(src)->s_addr);
sprintf(dst, "%u.%u.%u.%u",
(he >> 24) & 0xFF,
(he >> 16) & 0xFF,
(he >> 8) & 0xFF,
(he >> 0) & 0xFF
);
return dst;
}
errno = EAFNOSUPPORT;
return nullptr;
}