Skip to content

Instantly share code, notes, and snippets.

@keddad
Created June 19, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keddad/2fe20124d8ca7f4706820a25cbaf2f3a to your computer and use it in GitHub Desktop.
Save keddad/2fe20124d8ca7f4706820a25cbaf2f3a to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
int main() {
struct addrinfo *res = nullptr;
getaddrinfo("google.com", "443", nullptr, &res);
struct addrinfo *i;
for (i = res; i != nullptr; i = i->ai_next) {
char str[INET6_ADDRSTRLEN];
if (i->ai_addr->sa_family == AF_INET) {
auto *p = (struct sockaddr_in *) i->ai_addr;
printf("%s\n", inet_ntop(AF_INET, &p->sin_addr, str, sizeof(str)));
} else if (i->ai_addr->sa_family == AF_INET6) {
auto *p = (struct sockaddr_in6 *) i->ai_addr;
printf("%s\n", inet_ntop(AF_INET6, &p->sin6_addr, str, sizeof(str)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment