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