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