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