hostent2.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. char *ptr, **pptr;
  6. char str[INET6_ADDRSTRLEN];
  7. struct hostent *hptr;
  8. while (--argc > 0) {
  9. ptr = *++argv;
  10. if ( (hptr = gethostbyname(ptr)) == NULL) {
  11. err_msg("gethostbyname error for host: %s: %s",
  12. ptr, hstrerror(h_errno));
  13. continue;
  14. }
  15. printf("official hostname: %s\n", hptr->h_name);
  16. for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
  17. printf(" alias: %s\n", *pptr);
  18. switch (hptr->h_addrtype) {
  19. case AF_INET:
  20. #ifdef AF_INET6
  21. case AF_INET6:
  22. #endif
  23. pptr = hptr->h_addr_list;
  24. for ( ; *pptr != NULL; pptr++) {
  25. printf("\taddress: %s\n",
  26. Inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
  27. if ( (hptr = gethostbyaddr(*pptr, hptr->h_length,
  28. hptr->h_addrtype)) == NULL)
  29. printf("\t(gethostbyaddr failed)\n");
  30. else if (hptr->h_name != NULL)
  31. printf("\tname = %s\n", hptr->h_name);
  32. else
  33. printf("\t(no hostname returned by gethostbyaddr)\n");
  34. }
  35. break;
  36. default:
  37. err_ret("unknown address type");
  38. break;
  39. }
  40. }
  41. exit(0);
  42. }