diff --git a/Jupiter/Socket.cpp b/Jupiter/Socket.cpp index cf8f448..c1bd591 100644 --- a/Jupiter/Socket.cpp +++ b/Jupiter/Socket.cpp @@ -316,6 +316,35 @@ in_addr6 Jupiter::Socket::pton6(const char *str) return r; } +Jupiter::StringS Jupiter::Socket::ntop4(uint32_t ip) +{ + static char buf[16]; + if (inet_ntop(AF_INET, &ip, buf, sizeof(buf)) == nullptr) + return Jupiter::StringS::empty; + return Jupiter::String(buf); +} + +Jupiter::StringS Jupiter::Socket::ntop6(in_addr6 ip) +{ + static char buf[46]; + if (inet_ntop(AF_INET6, &ip, buf, sizeof(buf)) == nullptr) + return Jupiter::StringS::empty; + return Jupiter::String(buf); +} + +Jupiter::StringS Jupiter::Socket::ntop(void *ip, size_t size) +{ + switch (size) + { + case 4: + return ntop4(*reinterpret_cast(ip)); + case 16: + return ntop6(*reinterpret_cast(ip)); + default: + return Jupiter::StringS::empty; + } +} + Jupiter::Socket *Jupiter::Socket::acceptConnection() { SOCKET tSock = accept(Socket::data_->rawSock, 0, 0); diff --git a/Jupiter/Socket.h b/Jupiter/Socket.h index 06498c0..a6c0522 100644 --- a/Jupiter/Socket.h +++ b/Jupiter/Socket.h @@ -26,6 +26,7 @@ #include #include "Jupiter.h" #include "Readable_String.h" +#include "String.h" struct addrinfo; struct in_addr6; @@ -180,6 +181,32 @@ namespace Jupiter */ static in_addr6 pton6(const char *str); + /** + * @brief Formats an IPv4 address in its string presentation format. + * + * @param ip IP to format + * @return String containing the address's string presentation. + */ + static Jupiter::StringS ntop4(uint32_t ip); + + /** + * @brief Formats an IPv6 address in its string presentation format. + * + * @param ip IP to format + * @return String containing the address's string presentation. + */ + static Jupiter::StringS ntop6(in_addr6 ip); + + /** + * @brief Formats an IPvX address in its string presentation format. + * Currently supports: IPv4, IPv6. + * + * @param ip IP to format + * @param size Size of the input type + * @return String containing the address's string presentation. + */ + static Jupiter::StringS ntop(void *ip, size_t size); + /** * @brief Interface to provide simple connection establishing. * Note: When using this method, the hostname and port are NOT stored; thus getHost(), getHostname(), diff --git a/Release/Jupiter.lib b/Release/Jupiter.lib index ec0085b..810108f 100644 Binary files a/Release/Jupiter.lib and b/Release/Jupiter.lib differ