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