From 6fb69a1dc24d3551db397eda3aedfa522b91e76c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 8 Feb 2024 11:59:11 +0200 Subject: [PATCH] LibC: Implement inet_ntop for IPv4 addresses --- libc/arpa/inet.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libc/arpa/inet.cpp b/libc/arpa/inet.cpp index e81ef385..a7beed25 100644 --- a/libc/arpa/inet.cpp +++ b/libc/arpa/inet.cpp @@ -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(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; +}