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