/* gethostbyaddr.c: illustrate the use of gethostbyaddr function. compile: cc -c gethostbyaddr.c -lnsl */ #include #include #include #include #include #include #include #include int main() { unsigned long addr; char haddr[100]; struct hostent *hp; /* host pointer */ char **p; /* help... */ int help; printf("Enter host address in dotted decimal notation...\n"); printf(" - Try cssun's numerical address (170.140.150.1)...\n"); printf(">>> "); scanf("%s", haddr); addr = inet_addr(haddr); /* Convert dotted notation to bits */ if (addr == -1) { printf("IP-address must be of the form a.b.c.d\n"); exit(1); } printf("\nDotted notation: %s, actual address: %u\n\n", haddr, addr); hp = gethostbyaddr((char*) &addr, sizeof(addr), AF_INET); if (hp == NULL) { printf("host information for %s not found\n", haddr); exit(1); } printf("official hostname h_name = `%s'\n", hp->h_name); printf("alias list:\n"); for (p = hp->h_aliases; *p != 0; p++) { printf("\t%s\n", *p); } printf("address type = %d (should be AF_INET = %d)\n", hp->h_addrtype, AF_INET); printf("address length = %d\n", hp->h_length); printf("list of Internet addresses for host:\n"); for (p = hp->h_addr_list; *p != 0; p++) { struct in_addr x; memcpy(&x.s_addr, *p, sizeof(x.s_addr)); help = x.s_addr; printf("\tdotted: %s (unsigned decimal: %u)\n", inet_ntoa(x), help); } }