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