diff --git a/Jupiter/Socket.cpp b/Jupiter/Socket.cpp index 7a25404..fb5eac5 100644 --- a/Jupiter/Socket.cpp +++ b/Jupiter/Socket.cpp @@ -324,6 +324,13 @@ char *Jupiter::Socket::resolveAddress(const char *hostname, unsigned int result) char *Jupiter::Socket::resolveHostname(addrinfo *addr) // static { static char resolved[NI_MAXHOST]; + getnameinfo(addr->ai_addr, addr->ai_addrlen, resolved, sizeof(resolved), 0, 0, 0); + return resolved; +} + +char *Jupiter::Socket::resolveHostname_alloc(addrinfo *addr) // static +{ + char *resolved = new char[NI_MAXHOST]; getnameinfo(addr->ai_addr, addr->ai_addrlen, resolved, NI_MAXHOST, 0, 0, 0); return resolved; } @@ -335,6 +342,13 @@ char *Jupiter::Socket::resolveHostname(addrinfo *addr, unsigned int result) // s return Jupiter::Socket::resolveHostname(ptr); } +char *Jupiter::Socket::resolveHostname_alloc(addrinfo *addr, unsigned int result) // static +{ + addrinfo *ptr = Jupiter::Socket::getAddrInfo(addr, result); + if (ptr == nullptr) return nullptr; + return Jupiter::Socket::resolveHostname_alloc(ptr); +} + char *Jupiter::Socket::resolveHostname(const char *hostname, unsigned int result) // static { addrinfo *info = Jupiter::Socket::getAddrInfo(hostname, 0); @@ -342,6 +356,13 @@ char *Jupiter::Socket::resolveHostname(const char *hostname, unsigned int result return Jupiter::Socket::resolveHostname(info, result); } +char *Jupiter::Socket::resolveHostname_alloc(const char *hostname, unsigned int result) // static +{ + addrinfo *info = Jupiter::Socket::getAddrInfo(hostname, 0); + if (info == nullptr) return nullptr; + return Jupiter::Socket::resolveHostname_alloc(info, result); +} + uint32_t Jupiter::Socket::pton4(const char *str) { in_addr r; diff --git a/Jupiter/Socket.h b/Jupiter/Socket.h index b83faa8..6ead2f2 100644 --- a/Jupiter/Socket.h +++ b/Jupiter/Socket.h @@ -126,11 +126,13 @@ namespace Jupiter /** * @brief Resolves an address to a hostname. (Reverse DNS) + * Note: the '_alloc' version of this does NOT use a static buffer. * * @param addr Address info containing IP address. * @return String containing the hostname of the stored address on success, nullptr otherwise. */ static char *resolveHostname(addrinfo *addr); + static char *resolveHostname_alloc(addrinfo *addr); /** * @brief Resolves an address to a hostname. (Reverse DNS) @@ -140,6 +142,7 @@ namespace Jupiter * @return String containing the hostname of the stored address on success, nullptr otherwise. */ static char *resolveHostname(addrinfo *addr, unsigned int result); + static char *resolveHostname_alloc(addrinfo *addr, unsigned int result); /** * @brief Resolves an address to a hostname. (Reverse DNS) @@ -149,6 +152,7 @@ namespace Jupiter * @return String containing the hostname of the resolved address on success, nullptr otherwise. */ static char *resolveHostname(const char *hostname, unsigned int result); + static char *resolveHostname_alloc(const char *hostname, unsigned int result); /** * @brief Reinterprets an IPv4 address as a 32-bit integer in network byte order. diff --git a/Release/Jupiter.lib b/Release/Jupiter.lib index 0978f35..216219b 100644 Binary files a/Release/Jupiter.lib and b/Release/Jupiter.lib differ