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