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