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