Zurück zur SP2 Übersicht
dns.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Syntax: %s <hostname>\n", argv[0]);
exit(EXIT_FAILURE);
}
char *hostname = argv[1];
struct addrinfo hints = {
.ai_socktype = SOCK_STREAM,
.ai_family = AF_UNSPEC, .ai_flags = AI_ADDRCONFIG,
};
struct addrinfo *head;
getaddrinfo(hostname, "80", &hints, &head);
struct addrinfo *curr;
for (curr = head; curr != NULL; curr = curr->ai_next) {
char buffer[64];
void *tmp;
switch (curr->ai_addr->sa_family) {
case AF_INET:
tmp = &((struct sockaddr_in*) curr->ai_addr)->sin_addr;
printf("IPv4 address: %s\n", inet_ntop(AF_INET, tmp, (char * restrict) &buffer, 64));
break;
case AF_INET6:
tmp = &((struct sockaddr_in6*) curr->ai_addr)->sin6_addr;
printf("IPv6 address: %s\n", inet_ntop(AF_INET6, tmp, (char * restrict) &buffer, 64));
break;
default:
printf("Neither IPv4 nor IPv6.\n");
}
}
freeaddrinfo(head);
}