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