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