Home | History | Annotate | Line # | Download | only in net
ethers.c revision 1.1
      1  1.1  deraadt /*
      2  1.1  deraadt  * ethers(3N) a la Sun.
      3  1.1  deraadt  *
      4  1.1  deraadt  * Written by Roland McGrath <roland (at) frob.com> 10/14/93.
      5  1.1  deraadt  * Public domain.
      6  1.1  deraadt  *
      7  1.1  deraadt  * $Id: ethers.c,v 1.1 1993/12/16 05:17:37 deraadt Exp $
      8  1.1  deraadt  */
      9  1.1  deraadt 
     10  1.1  deraadt #include <sys/types.h>
     11  1.1  deraadt #include <sys/socket.h>
     12  1.1  deraadt #include <net/if.h>
     13  1.1  deraadt #include <netinet/in.h>
     14  1.1  deraadt #include <netinet/if_ether.h>
     15  1.1  deraadt #include <sys/param.h>
     16  1.1  deraadt #include <paths.h>
     17  1.1  deraadt #include <errno.h>
     18  1.1  deraadt #include <stdio.h>
     19  1.1  deraadt 
     20  1.1  deraadt #ifndef _PATH_ETHERS
     21  1.1  deraadt #define _PATH_ETHERS "/etc/ethers"
     22  1.1  deraadt #endif
     23  1.1  deraadt 
     24  1.1  deraadt /*
     25  1.1  deraadt  * Sun uses this structure in netinet/if_ether.h.
     26  1.1  deraadt  * It looked like it would be harmless to change that file,
     27  1.1  deraadt  * but I didn't want to bother and struct ether_addr and u_char[6]
     28  1.1  deraadt  * are layed out identically in memory anyway.
     29  1.1  deraadt  */
     30  1.1  deraadt struct ether_addr {
     31  1.1  deraadt 	u_char ether_addr_octet[6];
     32  1.1  deraadt };
     33  1.1  deraadt 
     34  1.1  deraadt char *
     35  1.1  deraadt ether_ntoa(e)
     36  1.1  deraadt 	struct ether_addr *e;
     37  1.1  deraadt {
     38  1.1  deraadt 	static char a[] = "xx:xx:xx:xx:xx:xx";
     39  1.1  deraadt 
     40  1.1  deraadt 	sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
     41  1.1  deraadt 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     42  1.1  deraadt 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
     43  1.1  deraadt 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
     44  1.1  deraadt 	return a;
     45  1.1  deraadt }
     46  1.1  deraadt 
     47  1.1  deraadt struct ether_addr *
     48  1.1  deraadt ether_aton(s)
     49  1.1  deraadt 	char *s;
     50  1.1  deraadt {
     51  1.1  deraadt 	static struct ether_addr n;
     52  1.1  deraadt 	u_int i[6];
     53  1.1  deraadt 
     54  1.1  deraadt 	if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
     55  1.1  deraadt 	    &i[2], &i[3], &i[4], &i[5]) == 6) {
     56  1.1  deraadt 		n.ether_addr_octet[0] = (u_char)i[0];
     57  1.1  deraadt 		n.ether_addr_octet[1] = (u_char)i[1];
     58  1.1  deraadt 		n.ether_addr_octet[2] = (u_char)i[2];
     59  1.1  deraadt 		n.ether_addr_octet[3] = (u_char)i[3];
     60  1.1  deraadt 		n.ether_addr_octet[4] = (u_char)i[4];
     61  1.1  deraadt 		n.ether_addr_octet[5] = (u_char)i[5];
     62  1.1  deraadt 		return &n;
     63  1.1  deraadt 	}
     64  1.1  deraadt 	return NULL;
     65  1.1  deraadt }
     66  1.1  deraadt 
     67  1.1  deraadt ether_ntohost(hostname, e)
     68  1.1  deraadt 	char *hostname;
     69  1.1  deraadt 	struct ether_addr *e;
     70  1.1  deraadt {
     71  1.1  deraadt 	FILE *f;
     72  1.1  deraadt 	char buf[BUFSIZ];
     73  1.1  deraadt 	struct ether_addr try;
     74  1.1  deraadt 
     75  1.1  deraadt #ifdef YP
     76  1.1  deraadt 	char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
     77  1.1  deraadt 	int trylen;
     78  1.1  deraadt 
     79  1.1  deraadt 	sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
     80  1.1  deraadt 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     81  1.1  deraadt 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
     82  1.1  deraadt 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
     83  1.1  deraadt 	trylen = strlen(trybuf);
     84  1.1  deraadt #endif
     85  1.1  deraadt 
     86  1.1  deraadt 	f = fopen(_PATH_ETHERS, "r");
     87  1.1  deraadt 	if (f==NULL)
     88  1.1  deraadt 		return -1;
     89  1.1  deraadt 	while (fgets(buf, sizeof buf, f)) {
     90  1.1  deraadt #ifdef YP
     91  1.1  deraadt 		/* A + in the file means try YP now.  */
     92  1.1  deraadt 		if (!strncmp(buf, "+\n", sizeof buf)) {
     93  1.1  deraadt 			char *ypbuf, *ypdom;
     94  1.1  deraadt 			int ypbuflen;
     95  1.1  deraadt 
     96  1.1  deraadt 			if (yp_get_default_domain(&ypdom))
     97  1.1  deraadt 				continue;
     98  1.1  deraadt 			if (yp_match(ypdom, "ethers.byaddr", trybuf,
     99  1.1  deraadt 			    trylen, &ypbuf, &ypbuflen))
    100  1.1  deraadt 				continue;
    101  1.1  deraadt 			if (ether_line(ypbuf, &try, hostname) == 0) {
    102  1.1  deraadt 				free(ypbuf);
    103  1.1  deraadt 				(void)fclose(f);
    104  1.1  deraadt 				return 0;
    105  1.1  deraadt 			}
    106  1.1  deraadt 			free(ypbuf);
    107  1.1  deraadt 		}
    108  1.1  deraadt #endif
    109  1.1  deraadt 		if (ether_line(buf, &try, hostname) == 0 &&
    110  1.1  deraadt 		    bcmp((char *)&try, (char *)e, sizeof try) == 0) {
    111  1.1  deraadt 			(void)fclose(f);
    112  1.1  deraadt 			return 0;
    113  1.1  deraadt 		}
    114  1.1  deraadt 	}
    115  1.1  deraadt 	(void)fclose(f);
    116  1.1  deraadt 	errno = ENOENT;
    117  1.1  deraadt 	return -1;
    118  1.1  deraadt }
    119  1.1  deraadt 
    120  1.1  deraadt ether_hostton(hostname, e)
    121  1.1  deraadt 	char *hostname;
    122  1.1  deraadt 	struct ether_addr *e;
    123  1.1  deraadt {
    124  1.1  deraadt 	FILE *f;
    125  1.1  deraadt 	char buf[BUFSIZ];
    126  1.1  deraadt 	char try[MAXHOSTNAMELEN];
    127  1.1  deraadt #ifdef YP
    128  1.1  deraadt 	int hostlen = strlen(hostname);
    129  1.1  deraadt #endif
    130  1.1  deraadt 
    131  1.1  deraadt 	f = fopen(_PATH_ETHERS, "r");
    132  1.1  deraadt 	if (f==NULL)
    133  1.1  deraadt 		return -1;
    134  1.1  deraadt 
    135  1.1  deraadt 	while (fgets(buf, sizeof buf, f)) {
    136  1.1  deraadt #ifdef YP
    137  1.1  deraadt 		/* A + in the file means try YP now.  */
    138  1.1  deraadt 		if (!strncmp(buf, "+\n", sizeof buf)) {
    139  1.1  deraadt 			char *ypbuf, *ypdom;
    140  1.1  deraadt 			int ypbuflen;
    141  1.1  deraadt 
    142  1.1  deraadt 			if (yp_get_default_domain(&ypdom))
    143  1.1  deraadt 				continue;
    144  1.1  deraadt 			if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
    145  1.1  deraadt 			    &ypbuf, &ypbuflen))
    146  1.1  deraadt 				continue;
    147  1.1  deraadt 			if (ether_line(ypbuf, e, try) == 0) {
    148  1.1  deraadt 				free(ypbuf);
    149  1.1  deraadt 				(void)fclose(f);
    150  1.1  deraadt 				return 0;
    151  1.1  deraadt 			}
    152  1.1  deraadt 			free(ypbuf);
    153  1.1  deraadt 		}
    154  1.1  deraadt #endif
    155  1.1  deraadt 		if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
    156  1.1  deraadt 			(void)fclose(f);
    157  1.1  deraadt 			return 0;
    158  1.1  deraadt 		}
    159  1.1  deraadt 	}
    160  1.1  deraadt 	(void)fclose(f);
    161  1.1  deraadt 	errno = ENOENT;
    162  1.1  deraadt 	return -1;
    163  1.1  deraadt }
    164  1.1  deraadt 
    165  1.1  deraadt ether_line(l, e, hostname)
    166  1.1  deraadt 	char *l;
    167  1.1  deraadt 	struct ether_addr *e;
    168  1.1  deraadt 	char *hostname;
    169  1.1  deraadt {
    170  1.1  deraadt 	u_int i[6];
    171  1.1  deraadt 
    172  1.1  deraadt 	if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1],
    173  1.1  deraadt 	    &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
    174  1.1  deraadt 		e->ether_addr_octet[0] = (u_char)i[0];
    175  1.1  deraadt 		e->ether_addr_octet[1] = (u_char)i[1];
    176  1.1  deraadt 		e->ether_addr_octet[2] = (u_char)i[2];
    177  1.1  deraadt 		e->ether_addr_octet[3] = (u_char)i[3];
    178  1.1  deraadt 		e->ether_addr_octet[4] = (u_char)i[4];
    179  1.1  deraadt 		e->ether_addr_octet[5] = (u_char)i[5];
    180  1.1  deraadt 		return 0;
    181  1.1  deraadt 	}
    182  1.1  deraadt 	errno = EINVAL;
    183  1.1  deraadt 	return -1;
    184  1.1  deraadt }
    185