Skip to content

Instantly share code, notes, and snippets.

@kunigami
Created March 5, 2020 00: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 kunigami/aa7ff866749353ebdaf69d49c5c4a660 to your computer and use it in GitHub Desktop.
Save kunigami/aa7ff866749353ebdaf69d49c5c4a660 to your computer and use it in GitHub Desktop.
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // will point to the results
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_INET6; // IPv6
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
getaddrinfo("www.google.com", "https", &hints, &servinfo);
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)servinfo->ai_addr;
void *addr = &(ipv6->sin6_addr);
char ipstr[INET6_ADDRSTRLEN];
inet_ntop(servinfo->ai_family, addr, ipstr, sizeof ipstr);
printf("IP: %s\n", ipstr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment