CPP
61
netaddress
Guest on 20th June 2022 06:12:54 AM
/* -jk Exploring gethostbyname with the intent of wrappign it in an object */
/* Old School C headers for Socket stuff */
#include <stdio.h> /* stderr, stdout */
#include <netdb.h> /* hostent struct, gethostbyname() */
#include <arpa/inet.h> /* inet_ntoa() to format IP address */
#include <netinet/in.h> /* in_addr structure */
/* C++ headers */
#include <cstdlib>
#include <cstddef>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char **argv) {
struct hostent *host; /* host information */
struct in_addr h_addr; /* internet address */
int i=0;
if (argc != 2) {
fprintf(stderr, "USAGE: %s <inet_address>\n", argv[0]);
exit(1);
}
if ((host = gethostbyname(argv[1])) == NULL) {
fprintf(stderr, "(mini) nslookup failed on '%s'\n", argv[1]);
exit(1);
}
fprintf(stdout, " Hostname %s\n", host->h_name);
for (i=0; host->h_aliases[i] != '\0'; i++ ) {
fprintf(stdout, " Aliases %s\n", host->h_aliases[i]);
}
// for (i=0; host->h_addr_list[i] != '\0'; i++ ) {
h_addr.s_addr = *((unsigned long *) host->h_addr_list[i]);
fprintf(stdout, "Addresses %s\n", inet_ntoa(h_addr));
// }
exit(0);
}