PageRenderTime 30ms CodeModel.GetById 27ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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