Fix getaddrinfo hook

This commit is contained in:
Federico Cecchetto 2022-02-26 16:07:45 +01:00
parent d34ba1f424
commit d7e1626240
3 changed files with 40 additions and 5 deletions

View File

@ -129,19 +129,43 @@ namespace demonware
server = udp_servers.find(name); server = udp_servers.find(name);
} }
const auto result = getaddrinfo(name, service, hints, res);
if (!server) if (!server)
{ {
return result; return getaddrinfo(name, service, hints, res);
} }
auto address = reinterpret_cast<sockaddr_in*>(res[0]->ai_addr); const auto address = utils::memory::get_allocator()->allocate<sockaddr>();
address->sin_addr.s_addr = server->get_address(); const auto ai = utils::memory::get_allocator()->allocate<addrinfo>();
auto in_addr = reinterpret_cast<sockaddr_in*>(address);
in_addr->sin_addr.s_addr = server->get_address();
in_addr->sin_family = AF_INET;
ai->ai_family = AF_INET;
ai->ai_socktype = SOCK_STREAM;
ai->ai_addr = address;
ai->ai_addrlen = sizeof(sockaddr);
ai->ai_next = nullptr;
ai->ai_flags = 0;
ai->ai_protocol = 0;
ai->ai_canonname = const_cast<char*>(name);
*res = ai;
return 0; return 0;
} }
void freeaddrinfo_stub(addrinfo* ai)
{
if (!utils::memory::get_allocator()->find(ai))
{
return freeaddrinfo(ai);
}
utils::memory::get_allocator()->free(ai->ai_addr);
utils::memory::get_allocator()->free(ai);
}
int getpeername_stub(const SOCKET s, sockaddr* addr, socklen_t* addrlen) int getpeername_stub(const SOCKET s, sockaddr* addr, socklen_t* addrlen)
{ {
auto* server = find_server(s); auto* server = find_server(s);
@ -500,6 +524,7 @@ namespace demonware
if (function == "#20") return io::sendto_stub; if (function == "#20") return io::sendto_stub;
if (function == "#52") return io::gethostbyname_stub; if (function == "#52") return io::gethostbyname_stub;
if (function == "getaddrinfo") return io::getaddrinfo_stub; if (function == "getaddrinfo") return io::getaddrinfo_stub;
if (function == "freeaddrinfo") return io::freeaddrinfo_stub;
} }
if (function == "InternetGetConnectedState") if (function == "InternetGetConnectedState")

View File

@ -62,6 +62,14 @@ namespace utils
return data; return data;
} }
bool memory::allocator::find(const void* data)
{
std::lock_guard _(this->mutex_);
const auto j = std::find(this->pool_.begin(), this->pool_.end(), data);
return j != this->pool_.end();
}
void* memory::allocate(const size_t length) void* memory::allocate(const size_t length)
{ {
return calloc(length, 1); return calloc(length, 1);

View File

@ -37,6 +37,8 @@ namespace utils
char* duplicate_string(const std::string& string); char* duplicate_string(const std::string& string);
bool find(const void* data);
private: private:
std::mutex mutex_; std::mutex mutex_;
std::vector<void*> pool_; std::vector<void*> pool_;