/contrib/ntp/libntp/numtohost.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 38 lines · 23 code · 6 blank · 9 comment · 6 complexity · 32b3bc59b3cab5535cbdc708131091c6 MD5 · raw file

  1. /*
  2. * numtohost - convert network number to host name.
  3. */
  4. #include "ntp_fp.h"
  5. #include "ntp_stdlib.h"
  6. #include "lib_strbuf.h"
  7. #define LOOPBACKNET 0x7f000000
  8. #define LOOPBACKHOST 0x7f000001
  9. #define LOOPBACKNETMASK 0xff000000
  10. char *
  11. numtohost(
  12. u_int32 netnum
  13. )
  14. {
  15. char *bp;
  16. struct hostent *hp;
  17. /*
  18. * This is really gross, but saves lots of hanging looking for
  19. * hostnames for the radio clocks. Don't bother looking up
  20. * addresses on the loopback network except for the loopback
  21. * host itself.
  22. */
  23. if ((((ntohl(netnum) & LOOPBACKNETMASK) == LOOPBACKNET)
  24. && (ntohl(netnum) != LOOPBACKHOST))
  25. || ((hp = gethostbyaddr((char *)&netnum, sizeof netnum, AF_INET))
  26. == 0))
  27. return numtoa(netnum);
  28. LIB_GETBUF(bp);
  29. bp[LIB_BUFLENGTH-1] = '\0';
  30. (void) strncpy(bp, hp->h_name, LIB_BUFLENGTH-1);
  31. return bp;
  32. }