drm/linux: Add kasprintf and kvasprintf()
[dragonfly.git] / libexec / bootpd / lookup.c
blob8af0b78db3bfa17791062e7583785f334ae213b6
1 /*
2 * lookup.c - Lookup IP address, HW address, netmask
4 * $FreeBSD: src/libexec/bootpd/lookup.c,v 1.7 1999/08/28 00:09:19 peter Exp $
5 */
7 #include <sys/types.h>
8 #include <sys/socket.h>
10 #include <sys/time.h> /* for struct timeval in net/if.h */
11 #include <net/if.h>
12 #include <netinet/in.h>
14 #ifdef ETC_ETHERS
15 #include <net/ethernet.h>
16 extern int ether_hostton();
17 #endif
19 #include <netdb.h>
20 #include <syslog.h>
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
28 #include "bootp.h"
29 #include "lookup.h"
30 #include "report.h"
33 * Lookup an Ethernet address and return it.
34 * Return NULL if addr not found.
36 u_char *
37 lookup_hwa(char *hostname, int htype)
39 switch (htype) {
41 /* XXX - How is this done on other systems? -gwr */
42 #ifdef ETC_ETHERS
43 case HTYPE_ETHERNET:
44 case HTYPE_IEEE802:
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;
53 return (u_char *) & ea;
55 #endif /* ETC_ETHERS */
57 default:
58 report(LOG_ERR, "no lookup for HW addr type %d", htype);
59 } /* switch */
61 /* If the system can't do it, just return an error. */
62 return NULL;
67 * Lookup an IP address.
68 * Return non-zero on failure.
70 int
71 lookup_ipa(char *hostname, u_int32 *result)
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;
83 * Lookup a netmask
84 * addr and result are both in network order. Return non-zero on failure.
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.
90 int
91 lookup_netmask(u_int32 addr, u_int32 *result)
93 int32 m, a;
95 a = ntohl(addr);
96 m = 0;
98 if (IN_CLASSA(a))
99 m = IN_CLASSA_NET;
101 if (IN_CLASSB(a))
102 m = IN_CLASSB_NET;
104 if (IN_CLASSC(a))
105 m = IN_CLASSC_NET;
107 if (!m)
108 return -1;
109 *result = htonl(m);
110 return 0;