Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / numtohost.c
blobb822bf4c34c3b5e5bfe2b6251e5147b72c3c3141
1 /* $NetBSD$ */
3 /*
4 * numtohost - convert network number to host name.
5 */
6 #include <config.h>
8 #include <sys/types.h>
9 #ifdef HAVE_NETINET_IN_H
10 #include <netinet/in.h> /* ntohl */
11 #endif
13 #include "ntp_fp.h"
14 #include "ntp_stdlib.h"
15 #include "lib_strbuf.h"
17 #define LOOPBACKNET 0x7f000000
18 #define LOOPBACKHOST 0x7f000001
19 #define LOOPBACKNETMASK 0xff000000
21 char *
22 numtohost(
23 u_int32 netnum
26 char *bp;
27 struct hostent *hp;
30 * This is really gross, but saves lots of hanging looking for
31 * hostnames for the radio clocks. Don't bother looking up
32 * addresses on the loopback network except for the loopback
33 * host itself.
35 if ((((ntohl(netnum) & LOOPBACKNETMASK) == LOOPBACKNET)
36 && (ntohl(netnum) != LOOPBACKHOST))
37 || ((hp = gethostbyaddr((char *)&netnum, sizeof netnum, AF_INET))
38 == 0))
39 return numtoa(netnum);
41 LIB_GETBUF(bp);
43 bp[LIB_BUFLENGTH-1] = '\0';
44 (void) strncpy(bp, hp->h_name, LIB_BUFLENGTH-1);
45 return bp;