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