]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/libntp/numtohost.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / libntp / numtohost.c
1 /*
2  * numtohost - convert network number to host name.
3  */
4 #include <config.h>
5
6 #include <sys/types.h>
7 #ifdef HAVE_NETINET_IN_H
8 #include <netinet/in.h>         /* ntohl */
9 #endif
10
11 #include "ntp_fp.h"
12 #include "ntp_stdlib.h"
13 #include "lib_strbuf.h"
14
15 #define LOOPBACKNET     0x7f000000
16 #define LOOPBACKHOST    0x7f000001
17 #define LOOPBACKNETMASK 0xff000000
18
19 char *
20 numtohost(
21         u_int32 netnum
22         )
23 {
24         char *bp;
25         struct hostent *hp;
26
27         /*
28          * This is really gross, but saves lots of hanging looking for
29          * hostnames for the radio clocks.  Don't bother looking up
30          * addresses on the loopback network except for the loopback
31          * host itself.
32          */
33         if ((((ntohl(netnum) & LOOPBACKNETMASK) == LOOPBACKNET)
34              && (ntohl(netnum) != LOOPBACKHOST))
35             || ((hp = gethostbyaddr((char *)&netnum, sizeof netnum, AF_INET))
36                 == 0))
37             return numtoa(netnum);
38         
39         LIB_GETBUF(bp);
40         strlcpy(bp, hp->h_name, LIB_BUFLENGTH);
41
42         return bp;
43 }