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