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