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