lookup.c revision 3229
1/*
2 * lookup.c - Lookup IP address, HW address, netmask
3 */
4
5#include <sys/types.h>
6#include <sys/socket.h>
7
8#include <net/if.h>
9#include <netinet/in.h>
10
11#ifdef	ETC_ETHERS
12#include <netinet/if_ether.h>
13extern int ether_hostton();
14#endif
15
16#include <netdb.h>
17#include <syslog.h>
18
19#ifndef USE_BFUNCS
20#include <memory.h>
21/* Yes, memcpy is OK here (no overlapped copies). */
22#define bcopy(a,b,c)    memcpy(b,a,c)
23#endif
24
25#include "bootp.h"
26#include "lookup.h"
27#include "report.h"
28
29/*
30 * Lookup an Ethernet address and return it.
31 * Return NULL if addr not found.
32 */
33u_char *
34lookup_hwa(hostname, htype)
35	char *hostname;
36	int htype;
37{
38	switch (htype) {
39
40		/* XXX - How is this done on other systems? -gwr */
41#ifdef	ETC_ETHERS
42	case HTYPE_ETHERNET:
43	case HTYPE_IEEE802:
44		{
45			static struct ether_addr ea;
46			/* This does a lookup in /etc/ethers */
47			if (ether_hostton(hostname, &ea)) {
48				report(LOG_ERR, "no HW addr for host \"%s\"",
49					   hostname);
50				return (u_char *) 0;
51			}
52			return (u_char *) & ea;
53		}
54#endif /* ETC_ETHERS */
55
56	default:
57		report(LOG_ERR, "no lookup for HW addr type %d", htype);
58	}							/* switch */
59
60	/* If the system can't do it, just return an error. */
61	return (u_char *) 0;
62}
63
64
65/*
66 * Lookup an IP address.
67 * Return non-zero on failure.
68 */
69int
70lookup_ipa(hostname, result)
71	char *hostname;
72	u_int32 *result;
73{
74	struct hostent *hp;
75	hp = gethostbyname(hostname);
76	if (!hp)
77		return -1;
78	bcopy(hp->h_addr, result, sizeof(*result));
79	return 0;
80}
81
82
83/*
84 * Lookup a netmask
85 * Return non-zero on failure.
86 *
87 * XXX - This is OK as a default, but to really make this automatic,
88 * we would need to get the subnet mask from the ether interface.
89 * If this is wrong, specify the correct value in the bootptab.
90 */
91int
92lookup_netmask(addr, result)
93	u_int32 addr;				/* both in network order */
94	u_int32 *result;
95{
96	int32 m, a;
97
98	a = ntohl(addr);
99	m = 0;
100
101	if (IN_CLASSA(a))
102		m = IN_CLASSA_NET;
103
104	if (IN_CLASSB(a))
105		m = IN_CLASSB_NET;
106
107	if (IN_CLASSC(a))
108		m = IN_CLASSC_NET;
109
110	if (!m)
111		return -1;
112	*result = htonl(m);
113	return 0;
114}
115
116/*
117 * Local Variables:
118 * tab-width: 4
119 * c-indent-level: 4
120 * c-argdecl-indent: 4
121 * c-continued-statement-offset: 4
122 * c-continued-brace-offset: -4
123 * c-label-offset: -4
124 * c-brace-offset: 0
125 * End:
126 */
127