lookup.c revision 18471
13229Spst/*
23229Spst * lookup.c - Lookup IP address, HW address, netmask
318471Swosch *
418471Swosch *	$Id$
53229Spst */
63229Spst
73229Spst#include <sys/types.h>
83229Spst#include <sys/socket.h>
93229Spst
1013572Spst#ifdef _AIX32
1113572Spst#include <sys/time.h>	/* for struct timeval in net/if.h */
1213572Spst#endif
133229Spst#include <net/if.h>
143229Spst#include <netinet/in.h>
153229Spst
163229Spst#ifdef	ETC_ETHERS
173229Spst#include <netinet/if_ether.h>
183229Spstextern int ether_hostton();
193229Spst#endif
203229Spst
213229Spst#include <netdb.h>
223229Spst#include <syslog.h>
233229Spst
243229Spst#ifndef USE_BFUNCS
253229Spst#include <memory.h>
263229Spst/* Yes, memcpy is OK here (no overlapped copies). */
273229Spst#define bcopy(a,b,c)    memcpy(b,a,c)
283229Spst#endif
293229Spst
303229Spst#include "bootp.h"
313229Spst#include "lookup.h"
323229Spst#include "report.h"
333229Spst
343229Spst/*
353229Spst * Lookup an Ethernet address and return it.
363229Spst * Return NULL if addr not found.
373229Spst */
383229Spstu_char *
393229Spstlookup_hwa(hostname, htype)
403229Spst	char *hostname;
413229Spst	int htype;
423229Spst{
433229Spst	switch (htype) {
443229Spst
453229Spst		/* XXX - How is this done on other systems? -gwr */
463229Spst#ifdef	ETC_ETHERS
473229Spst	case HTYPE_ETHERNET:
483229Spst	case HTYPE_IEEE802:
493229Spst		{
503229Spst			static struct ether_addr ea;
513229Spst			/* This does a lookup in /etc/ethers */
523229Spst			if (ether_hostton(hostname, &ea)) {
533229Spst				report(LOG_ERR, "no HW addr for host \"%s\"",
543229Spst					   hostname);
553229Spst				return (u_char *) 0;
563229Spst			}
573229Spst			return (u_char *) & ea;
583229Spst		}
593229Spst#endif /* ETC_ETHERS */
603229Spst
613229Spst	default:
623229Spst		report(LOG_ERR, "no lookup for HW addr type %d", htype);
633229Spst	}							/* switch */
643229Spst
653229Spst	/* If the system can't do it, just return an error. */
663229Spst	return (u_char *) 0;
673229Spst}
683229Spst
693229Spst
703229Spst/*
713229Spst * Lookup an IP address.
723229Spst * Return non-zero on failure.
733229Spst */
743229Spstint
753229Spstlookup_ipa(hostname, result)
763229Spst	char *hostname;
773229Spst	u_int32 *result;
783229Spst{
793229Spst	struct hostent *hp;
803229Spst	hp = gethostbyname(hostname);
813229Spst	if (!hp)
823229Spst		return -1;
833229Spst	bcopy(hp->h_addr, result, sizeof(*result));
843229Spst	return 0;
853229Spst}
863229Spst
873229Spst
883229Spst/*
893229Spst * Lookup a netmask
903229Spst * Return non-zero on failure.
913229Spst *
923229Spst * XXX - This is OK as a default, but to really make this automatic,
933229Spst * we would need to get the subnet mask from the ether interface.
943229Spst * If this is wrong, specify the correct value in the bootptab.
953229Spst */
963229Spstint
973229Spstlookup_netmask(addr, result)
983229Spst	u_int32 addr;				/* both in network order */
993229Spst	u_int32 *result;
1003229Spst{
1013229Spst	int32 m, a;
1023229Spst
1033229Spst	a = ntohl(addr);
1043229Spst	m = 0;
1053229Spst
1063229Spst	if (IN_CLASSA(a))
1073229Spst		m = IN_CLASSA_NET;
1083229Spst
1093229Spst	if (IN_CLASSB(a))
1103229Spst		m = IN_CLASSB_NET;
1113229Spst
1123229Spst	if (IN_CLASSC(a))
1133229Spst		m = IN_CLASSC_NET;
1143229Spst
1153229Spst	if (!m)
1163229Spst		return -1;
1173229Spst	*result = htonl(m);
1183229Spst	return 0;
1193229Spst}
1203229Spst
1213229Spst/*
1223229Spst * Local Variables:
1233229Spst * tab-width: 4
1243229Spst * c-indent-level: 4
1253229Spst * c-argdecl-indent: 4
1263229Spst * c-continued-statement-offset: 4
1273229Spst * c-continued-brace-offset: -4
1283229Spst * c-label-offset: -4
1293229Spst * c-brace-offset: 0
1303229Spst * End:
1313229Spst */
132