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