Skip to content

Instantly share code, notes, and snippets.

@eyberg
Created May 10, 2024 14:37
Show Gist options
  • Save eyberg/1a21c9759d8b441a29d2ab7236c4e51b to your computer and use it in GitHub Desktop.
Save eyberg/1a21c9759d8b441a29d2ab7236c4e51b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(void) {
struct addrinfo* res = NULL;
getaddrinfo("google.com", "443", 0, &res);
struct addrinfo* i;
for(i=res; i!=NULL; i=i->ai_next) {
char str[INET6_ADDRSTRLEN];
if (i->ai_addr->sa_family == AF_INET) {
struct sockaddr_in *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) {
struct sockaddr_in6 *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