Home | History | Annotate | Line # | Download | only in net
ethers.c revision 1.21.42.1
      1  1.21.42.1      yamt /*	$NetBSD: ethers.c,v 1.21.42.1 2012/04/17 00:05:21 yamt Exp $	*/
      2        1.5       cgd 
      3        1.1   deraadt /*
      4        1.1   deraadt  * ethers(3N) a la Sun.
      5        1.1   deraadt  *
      6        1.1   deraadt  * Written by Roland McGrath <roland (at) frob.com> 10/14/93.
      7        1.1   deraadt  * Public domain.
      8        1.1   deraadt  */
      9       1.18    itojun 
     10       1.18    itojun #include <sys/cdefs.h>
     11       1.18    itojun #if defined(LIBC_SCCS) && !defined(lint)
     12  1.21.42.1      yamt __RCSID("$NetBSD: ethers.c,v 1.21.42.1 2012/04/17 00:05:21 yamt Exp $");
     13       1.18    itojun #endif /* LIBC_SCCS and not lint */
     14        1.1   deraadt 
     15       1.10       jtc #include "namespace.h"
     16       1.15     lukem #include <sys/param.h>
     17        1.1   deraadt #include <sys/socket.h>
     18       1.15     lukem 
     19        1.1   deraadt #include <net/if.h>
     20        1.8        is #include <net/if_ether.h>
     21        1.1   deraadt #include <netinet/in.h>
     22       1.15     lukem 
     23       1.15     lukem #include <assert.h>
     24       1.15     lukem #include <errno.h>
     25        1.1   deraadt #include <paths.h>
     26        1.1   deraadt #include <stdio.h>
     27        1.3       jtc #include <stdlib.h>
     28        1.3       jtc #include <string.h>
     29       1.15     lukem 
     30        1.9  christos #ifdef YP
     31        1.9  christos #include <rpcsvc/ypclnt.h>
     32       1.10       jtc #endif
     33       1.10       jtc 
     34       1.10       jtc #ifdef __weak_alias
     35       1.17   mycroft __weak_alias(ether_aton,_ether_aton)
     36       1.17   mycroft __weak_alias(ether_hostton,_ether_hostton)
     37       1.17   mycroft __weak_alias(ether_line,_ether_line)
     38       1.17   mycroft __weak_alias(ether_ntoa,_ether_ntoa)
     39       1.17   mycroft __weak_alias(ether_ntohost,_ether_ntohost)
     40        1.9  christos #endif
     41        1.1   deraadt 
     42        1.1   deraadt #ifndef _PATH_ETHERS
     43        1.1   deraadt #define _PATH_ETHERS "/etc/ethers"
     44        1.1   deraadt #endif
     45        1.1   deraadt 
     46        1.1   deraadt char *
     47  1.21.42.1      yamt ether_ntoa(const struct ether_addr *e)
     48        1.1   deraadt {
     49       1.13   mycroft 	static char a[18];
     50        1.1   deraadt 
     51       1.15     lukem 	_DIAGASSERT(e != NULL);
     52       1.15     lukem 
     53       1.20      tron 	(void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
     54        1.1   deraadt 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     55        1.1   deraadt 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
     56        1.1   deraadt 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
     57        1.1   deraadt 	return a;
     58        1.1   deraadt }
     59        1.1   deraadt 
     60        1.1   deraadt struct ether_addr *
     61  1.21.42.1      yamt ether_aton(const char *s)
     62        1.1   deraadt {
     63        1.1   deraadt 	static struct ether_addr n;
     64        1.1   deraadt 	u_int i[6];
     65        1.1   deraadt 
     66       1.15     lukem 	_DIAGASSERT(s != NULL);
     67       1.15     lukem 
     68        1.1   deraadt 	if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
     69        1.1   deraadt 	    &i[2], &i[3], &i[4], &i[5]) == 6) {
     70        1.1   deraadt 		n.ether_addr_octet[0] = (u_char)i[0];
     71        1.1   deraadt 		n.ether_addr_octet[1] = (u_char)i[1];
     72        1.1   deraadt 		n.ether_addr_octet[2] = (u_char)i[2];
     73        1.1   deraadt 		n.ether_addr_octet[3] = (u_char)i[3];
     74        1.1   deraadt 		n.ether_addr_octet[4] = (u_char)i[4];
     75        1.1   deraadt 		n.ether_addr_octet[5] = (u_char)i[5];
     76        1.1   deraadt 		return &n;
     77        1.1   deraadt 	}
     78        1.1   deraadt 	return NULL;
     79        1.1   deraadt }
     80        1.1   deraadt 
     81        1.6     mikel int
     82  1.21.42.1      yamt ether_ntohost(char *hostname, const struct ether_addr *e)
     83        1.1   deraadt {
     84        1.1   deraadt 	FILE *f;
     85       1.11     lukem 	char *p;
     86       1.12    mjacob 	size_t len;
     87        1.1   deraadt 	struct ether_addr try;
     88        1.1   deraadt #ifdef YP
     89        1.1   deraadt 	char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
     90        1.1   deraadt 	int trylen;
     91       1.15     lukem #endif
     92        1.1   deraadt 
     93       1.15     lukem 	_DIAGASSERT(hostname != NULL);
     94       1.15     lukem 	_DIAGASSERT(e != NULL);
     95       1.15     lukem 
     96       1.15     lukem #ifdef YP
     97       1.11     lukem 	trylen = snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x",
     98        1.1   deraadt 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     99        1.1   deraadt 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
    100        1.1   deraadt 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
    101        1.1   deraadt #endif
    102        1.1   deraadt 
    103        1.1   deraadt 	f = fopen(_PATH_ETHERS, "r");
    104       1.11     lukem 	if (f == NULL)
    105        1.1   deraadt 		return -1;
    106       1.11     lukem 	while ((p = fgetln(f, &len)) != NULL) {
    107       1.11     lukem 		if (p[len - 1] != '\n')
    108       1.11     lukem 			continue;		/* skip lines w/o \n */
    109       1.11     lukem 		p[--len] = '\0';
    110        1.1   deraadt #ifdef YP
    111        1.1   deraadt 		/* A + in the file means try YP now.  */
    112       1.11     lukem 		if (len == 1 && *p == '+') {
    113        1.1   deraadt 			char *ypbuf, *ypdom;
    114        1.1   deraadt 			int ypbuflen;
    115        1.1   deraadt 
    116        1.1   deraadt 			if (yp_get_default_domain(&ypdom))
    117        1.1   deraadt 				continue;
    118        1.1   deraadt 			if (yp_match(ypdom, "ethers.byaddr", trybuf,
    119        1.1   deraadt 			    trylen, &ypbuf, &ypbuflen))
    120        1.1   deraadt 				continue;
    121        1.1   deraadt 			if (ether_line(ypbuf, &try, hostname) == 0) {
    122        1.1   deraadt 				free(ypbuf);
    123        1.1   deraadt 				(void)fclose(f);
    124        1.1   deraadt 				return 0;
    125        1.1   deraadt 			}
    126        1.1   deraadt 			free(ypbuf);
    127        1.2   deraadt 			continue;
    128        1.1   deraadt 		}
    129        1.1   deraadt #endif
    130       1.11     lukem 		if (ether_line(p, &try, hostname) == 0 &&
    131       1.14  christos 		    memcmp(&try, e, sizeof try) == 0) {
    132        1.1   deraadt 			(void)fclose(f);
    133        1.1   deraadt 			return 0;
    134        1.1   deraadt 		}
    135        1.1   deraadt 	}
    136        1.1   deraadt 	(void)fclose(f);
    137        1.1   deraadt 	errno = ENOENT;
    138        1.1   deraadt 	return -1;
    139        1.1   deraadt }
    140        1.1   deraadt 
    141        1.6     mikel int
    142  1.21.42.1      yamt ether_hostton(const char *hostname, struct ether_addr *e)
    143        1.1   deraadt {
    144        1.1   deraadt 	FILE *f;
    145       1.11     lukem 	char *p;
    146       1.12    mjacob 	size_t len;
    147       1.11     lukem 	char try[MAXHOSTNAMELEN + 1];
    148        1.1   deraadt #ifdef YP
    149  1.21.42.1      yamt 	int hostlen = (int)strlen(hostname);
    150        1.1   deraadt #endif
    151        1.1   deraadt 
    152       1.15     lukem 	_DIAGASSERT(hostname != NULL);
    153       1.15     lukem 	_DIAGASSERT(e != NULL);
    154       1.15     lukem 
    155        1.1   deraadt 	f = fopen(_PATH_ETHERS, "r");
    156        1.1   deraadt 	if (f==NULL)
    157        1.1   deraadt 		return -1;
    158        1.1   deraadt 
    159       1.11     lukem 	while ((p = fgetln(f, &len)) != NULL) {
    160       1.11     lukem 		if (p[len - 1] != '\n')
    161       1.11     lukem 			continue;		/* skip lines w/o \n */
    162       1.11     lukem 		p[--len] = '\0';
    163        1.1   deraadt #ifdef YP
    164        1.1   deraadt 		/* A + in the file means try YP now.  */
    165       1.11     lukem 		if (len == 1 && *p == '+') {
    166        1.1   deraadt 			char *ypbuf, *ypdom;
    167        1.1   deraadt 			int ypbuflen;
    168        1.1   deraadt 
    169        1.1   deraadt 			if (yp_get_default_domain(&ypdom))
    170        1.1   deraadt 				continue;
    171        1.1   deraadt 			if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
    172        1.1   deraadt 			    &ypbuf, &ypbuflen))
    173        1.1   deraadt 				continue;
    174        1.1   deraadt 			if (ether_line(ypbuf, e, try) == 0) {
    175        1.1   deraadt 				free(ypbuf);
    176        1.1   deraadt 				(void)fclose(f);
    177        1.1   deraadt 				return 0;
    178        1.1   deraadt 			}
    179        1.1   deraadt 			free(ypbuf);
    180        1.2   deraadt 			continue;
    181        1.1   deraadt 		}
    182        1.1   deraadt #endif
    183       1.11     lukem 		if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
    184        1.1   deraadt 			(void)fclose(f);
    185        1.1   deraadt 			return 0;
    186        1.1   deraadt 		}
    187        1.1   deraadt 	}
    188        1.1   deraadt 	(void)fclose(f);
    189        1.1   deraadt 	errno = ENOENT;
    190        1.1   deraadt 	return -1;
    191        1.1   deraadt }
    192        1.1   deraadt 
    193        1.6     mikel int
    194  1.21.42.1      yamt ether_line(const char *l, struct ether_addr *e, char *hostname)
    195        1.1   deraadt {
    196        1.1   deraadt 	u_int i[6];
    197       1.15     lukem 
    198       1.19  sommerfe #define S2(arg) #arg
    199       1.19  sommerfe #define S1(arg) S2(arg)
    200       1.21    martin 	static const char fmt[] = " %x:%x:%x:%x:%x:%x"
    201       1.19  sommerfe 	    " %" S1(MAXHOSTNAMELEN) "s\n";
    202       1.19  sommerfe #undef S2
    203       1.19  sommerfe #undef S1
    204       1.19  sommerfe 
    205       1.15     lukem 	_DIAGASSERT(l != NULL);
    206       1.15     lukem 	_DIAGASSERT(e != NULL);
    207       1.15     lukem 	_DIAGASSERT(hostname != NULL);
    208        1.1   deraadt 
    209       1.19  sommerfe 	if (sscanf(l, fmt,
    210       1.11     lukem 	    &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
    211        1.1   deraadt 		e->ether_addr_octet[0] = (u_char)i[0];
    212        1.1   deraadt 		e->ether_addr_octet[1] = (u_char)i[1];
    213        1.1   deraadt 		e->ether_addr_octet[2] = (u_char)i[2];
    214        1.1   deraadt 		e->ether_addr_octet[3] = (u_char)i[3];
    215        1.1   deraadt 		e->ether_addr_octet[4] = (u_char)i[4];
    216        1.1   deraadt 		e->ether_addr_octet[5] = (u_char)i[5];
    217        1.1   deraadt 		return 0;
    218        1.1   deraadt 	}
    219        1.1   deraadt 	errno = EINVAL;
    220        1.1   deraadt 	return -1;
    221        1.1   deraadt }
    222