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