Created
August 7, 2024 17:52
-
-
Save malachib/fa9ec333c0d3bb8d2084f357145b5a6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Maybe https://stackoverflow.com/questions/67274905/c-c-resolving-docker-compose-services-to-same-ip can help | |
// https://man7.org/linux/man-pages/man3/getaddrinfo.3.html | |
template <class F> | |
bool getaddr(const char* host, const char* service, F&& f) | |
{ | |
addrinfo hints{}; | |
addrinfo* result; | |
//hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ | |
hints.ai_family = PF_INET; /* Allow IPv4 or IPv6 */ | |
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ | |
hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ | |
hints.ai_protocol = 0; /* Any protocol */ | |
hints.ai_canonname = NULL; | |
hints.ai_addr = NULL; | |
hints.ai_next = NULL; | |
int ret = getaddrinfo(host, service, &hints, &result); | |
if(ret != 0) | |
{ | |
std::cerr << gai_strerror(ret) << std::endl; | |
return false; | |
//exit(-1); | |
} | |
for(const addrinfo* i = result; i != nullptr; i = i->ai_next) | |
{ | |
f(*i); | |
} | |
freeaddrinfo(result); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment