Skip to content

Instantly share code, notes, and snippets.

@lwhsu

lwhsu/a.c Secret

Created January 21, 2018 19:17
Show Gist options
  • Save lwhsu/1288aa5be90b9e7da934a3e2bfc55aa3 to your computer and use it in GitHub Desktop.
Save lwhsu/1288aa5be90b9e7da934a3e2bfc55aa3 to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int err;
struct addrinfo hints, *res, *p;
const char *host = "pkg.freebsd.org";
const char *service = "http";
void *addr;
char *ipver;
struct sockaddr_in *ipv4;
struct sockaddr_in6 *ipv6;
char ipstr[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG;
err = getaddrinfo(host, service, &hints, &res);
if (err != 0)
errx(err, "getaddrinfo: %s", gai_strerror(err));
for (p = res; p != NULL; p = p->ai_next) {
if (p->ai_family == AF_INET) {
ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else if (p->ai_family == AF_INET6) {
ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
} else {
printf("unknown ai_family\n");
}
inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
printf(" %s: %s\n", ipver, ipstr);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment