/* MehdiJassmine 2018 * They must have read about some defaults, because it works without numerous * flags set * and they do not use all the recommended libraries, * I have added some comments, the key being to 'disect' man getaddrinfo * / #include "dnsaccess.h" int main(int argc, char **argv) { /* Check command line args */ if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } /* CreateAndType string, and then Pull domain name from input stream */ char* domain_name_or_address = argv[1]; /* My mistake, here they are converting an IP address to a Domain Name */ /* and then passing that on */ if (check_IP_address(domain_name_or_address)){ char* addr = domain_name_or_address; char hostname[1024]; /* call their own function for this stupid option */ get_hostname_from_addr(addr, hostname, sizeof(hostname)); printf("hostname: %s\n", hostname); } else { char* domain_name = domain_name_or_address; /* Mike comment */ /* from 'man getaddrinfo' the following code can be used to setup that * structure * memset(&hints, 0, sizeof(struct addrinfo)); * hints.ai_family = AF_UNSPEC; Allow IPv4 or IPv6 * hints.ai_socktype = SOCK_DGRAM; Datagram socket * hints.ai_flags = AI_PASSIVE; For wildcard IP address * hints.ai_protocol = 0; Any protocol * hints.ai_canonname = NULL; * hints.ai_addr = NULL; * hints.ai_next = NULL; */ /* Their code */ /* create the struct hints as a addrinfo type */ struct addrinfo hints; memset(&hints, 0, sizeof(hints)); /* Zero hints struct */ hints.ai_family = AF_INET; /* Only hint set, rest must be defaults */ /* Finds IP addresses associated to a domain name. */ struct addrinfo* result; getaddrinfo(domain_name, NULL, &hints, &result); /* Take the returned pointed to struct, result and pull it apart */ for(struct addrinfo* res = result; res != NULL; res = res->ai_next){ struct sockaddr_in* sa = (struct sockaddr_in*) res->ai_addr; struct in_addr ip_addr = sa->sin_addr; char* addr = inet_ntoa(ip_addr); printf("address: %s\n", addr); } } }