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