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