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