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