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