int_ghba_r.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * int_ghba_r.c:
  3. * Test program to see whether gethostbyaddr_r takes 8 arguments and returns
  4. * int.
  5. */
  6. static const char rcsid[] = "$Id: int_ghba_r.c,v 1.4 2011/10/03 18:19:13 pdw Exp $";
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <errno.h>
  10. #include <netdb.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <netinet/in.h>
  14. int main(void) {
  15. struct in_addr localhost;
  16. struct hostent hostbuf, *hp;
  17. char *buf;
  18. int res, herr;
  19. size_t buflen = 1024;
  20. localhost.s_addr = htonl(INADDR_LOOPBACK);
  21. buf = malloc(buflen);
  22. while ((res = gethostbyaddr_r((char*)&localhost, sizeof localhost, AF_INET,
  23. &hostbuf, buf, buflen, &hp, &herr))
  24. == ERANGE)
  25. buf = (char*)realloc(buf, buflen *= 2);
  26. /* We assume that the loopback address can always be resolved if
  27. * gethostbyaddr_r is actually working. */
  28. if (res || hp == NULL) {
  29. fprintf(stderr, "errno = %d, herr = %d, res = %d\n", errno, herr, res);
  30. return -1;
  31. } else
  32. return 0;
  33. }