2002-09-16 23:25:30 +04:00
|
|
|
/* $NetBSD: ethers.c,v 1.20 2002/09/16 19:25:33 tron Exp $ */
|
1995-02-25 09:20:26 +03:00
|
|
|
|
1993-12-16 08:17:35 +03:00
|
|
|
/*
|
|
|
|
* ethers(3N) a la Sun.
|
|
|
|
*
|
|
|
|
* Written by Roland McGrath <roland@frob.com> 10/14/93.
|
|
|
|
* Public domain.
|
|
|
|
*/
|
|
|
|
|
2000-04-24 14:40:24 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2002-09-16 23:25:30 +04:00
|
|
|
__RCSID("$NetBSD: ethers.c,v 1.20 2002/09/16 19:25:33 tron Exp $");
|
2000-04-24 14:40:24 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#include "namespace.h"
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <sys/param.h>
|
1993-12-16 08:17:35 +03:00
|
|
|
#include <sys/socket.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
|
1993-12-16 08:17:35 +03:00
|
|
|
#include <net/if.h>
|
1997-03-15 21:33:00 +03:00
|
|
|
#include <net/if_ether.h>
|
1993-12-16 08:17:35 +03:00
|
|
|
#include <netinet/in.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
1993-12-16 08:17:35 +03:00
|
|
|
#include <errno.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <paths.h>
|
1993-12-16 08:17:35 +03:00
|
|
|
#include <stdio.h>
|
1993-12-28 22:51:37 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
|
1997-07-13 23:57:30 +04:00
|
|
|
#ifdef YP
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
#endif
|
1993-12-16 08:17:35 +03:00
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#ifdef __weak_alias
|
2000-01-23 01:19:07 +03:00
|
|
|
__weak_alias(ether_aton,_ether_aton)
|
|
|
|
__weak_alias(ether_hostton,_ether_hostton)
|
|
|
|
__weak_alias(ether_line,_ether_line)
|
|
|
|
__weak_alias(ether_ntoa,_ether_ntoa)
|
|
|
|
__weak_alias(ether_ntohost,_ether_ntohost)
|
1997-07-21 18:06:24 +04:00
|
|
|
#endif
|
|
|
|
|
1993-12-16 08:17:35 +03:00
|
|
|
#ifndef _PATH_ETHERS
|
|
|
|
#define _PATH_ETHERS "/etc/ethers"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *
|
|
|
|
ether_ntoa(e)
|
2002-09-16 23:25:30 +04:00
|
|
|
const struct ether_addr *e;
|
1993-12-16 08:17:35 +03:00
|
|
|
{
|
1998-07-26 17:14:04 +04:00
|
|
|
static char a[18];
|
1993-12-16 08:17:35 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(e != NULL);
|
|
|
|
|
2002-09-16 23:25:30 +04:00
|
|
|
(void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
|
1993-12-16 08:17:35 +03:00
|
|
|
e->ether_addr_octet[0], e->ether_addr_octet[1],
|
|
|
|
e->ether_addr_octet[2], e->ether_addr_octet[3],
|
|
|
|
e->ether_addr_octet[4], e->ether_addr_octet[5]);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ether_addr *
|
|
|
|
ether_aton(s)
|
1997-11-02 17:21:24 +03:00
|
|
|
const char *s;
|
1993-12-16 08:17:35 +03:00
|
|
|
{
|
|
|
|
static struct ether_addr n;
|
|
|
|
u_int i[6];
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(s != NULL);
|
|
|
|
|
1993-12-16 08:17:35 +03:00
|
|
|
if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
|
|
|
|
&i[2], &i[3], &i[4], &i[5]) == 6) {
|
|
|
|
n.ether_addr_octet[0] = (u_char)i[0];
|
|
|
|
n.ether_addr_octet[1] = (u_char)i[1];
|
|
|
|
n.ether_addr_octet[2] = (u_char)i[2];
|
|
|
|
n.ether_addr_octet[3] = (u_char)i[3];
|
|
|
|
n.ether_addr_octet[4] = (u_char)i[4];
|
|
|
|
n.ether_addr_octet[5] = (u_char)i[5];
|
|
|
|
return &n;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-01-17 11:23:26 +03:00
|
|
|
int
|
1993-12-16 08:17:35 +03:00
|
|
|
ether_ntohost(hostname, e)
|
|
|
|
char *hostname;
|
2002-09-16 23:25:30 +04:00
|
|
|
const struct ether_addr *e;
|
1993-12-16 08:17:35 +03:00
|
|
|
{
|
|
|
|
FILE *f;
|
1997-11-02 17:21:24 +03:00
|
|
|
char *p;
|
1997-11-12 08:49:24 +03:00
|
|
|
size_t len;
|
1993-12-16 08:17:35 +03:00
|
|
|
struct ether_addr try;
|
|
|
|
#ifdef YP
|
|
|
|
char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
|
|
|
|
int trylen;
|
1999-09-16 15:44:54 +04:00
|
|
|
#endif
|
1993-12-16 08:17:35 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(hostname != NULL);
|
|
|
|
_DIAGASSERT(e != NULL);
|
|
|
|
|
|
|
|
#ifdef YP
|
1997-11-02 17:21:24 +03:00
|
|
|
trylen = snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x",
|
1993-12-16 08:17:35 +03:00
|
|
|
e->ether_addr_octet[0], e->ether_addr_octet[1],
|
|
|
|
e->ether_addr_octet[2], e->ether_addr_octet[3],
|
|
|
|
e->ether_addr_octet[4], e->ether_addr_octet[5]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
f = fopen(_PATH_ETHERS, "r");
|
1997-11-02 17:21:24 +03:00
|
|
|
if (f == NULL)
|
1993-12-16 08:17:35 +03:00
|
|
|
return -1;
|
1997-11-02 17:21:24 +03:00
|
|
|
while ((p = fgetln(f, &len)) != NULL) {
|
|
|
|
if (p[len - 1] != '\n')
|
|
|
|
continue; /* skip lines w/o \n */
|
|
|
|
p[--len] = '\0';
|
1993-12-16 08:17:35 +03:00
|
|
|
#ifdef YP
|
|
|
|
/* A + in the file means try YP now. */
|
1997-11-02 17:21:24 +03:00
|
|
|
if (len == 1 && *p == '+') {
|
1993-12-16 08:17:35 +03:00
|
|
|
char *ypbuf, *ypdom;
|
|
|
|
int ypbuflen;
|
|
|
|
|
|
|
|
if (yp_get_default_domain(&ypdom))
|
|
|
|
continue;
|
|
|
|
if (yp_match(ypdom, "ethers.byaddr", trybuf,
|
|
|
|
trylen, &ypbuf, &ypbuflen))
|
|
|
|
continue;
|
|
|
|
if (ether_line(ypbuf, &try, hostname) == 0) {
|
|
|
|
free(ypbuf);
|
|
|
|
(void)fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(ypbuf);
|
1993-12-16 22:19:14 +03:00
|
|
|
continue;
|
1993-12-16 08:17:35 +03:00
|
|
|
}
|
|
|
|
#endif
|
1997-11-02 17:21:24 +03:00
|
|
|
if (ether_line(p, &try, hostname) == 0 &&
|
1998-11-13 18:46:52 +03:00
|
|
|
memcmp(&try, e, sizeof try) == 0) {
|
1993-12-16 08:17:35 +03:00
|
|
|
(void)fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)fclose(f);
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-01-17 11:23:26 +03:00
|
|
|
int
|
1993-12-16 08:17:35 +03:00
|
|
|
ether_hostton(hostname, e)
|
1997-11-02 17:21:24 +03:00
|
|
|
const char *hostname;
|
1993-12-16 08:17:35 +03:00
|
|
|
struct ether_addr *e;
|
|
|
|
{
|
|
|
|
FILE *f;
|
1997-11-02 17:21:24 +03:00
|
|
|
char *p;
|
1997-11-12 08:49:24 +03:00
|
|
|
size_t len;
|
1997-11-02 17:21:24 +03:00
|
|
|
char try[MAXHOSTNAMELEN + 1];
|
1993-12-16 08:17:35 +03:00
|
|
|
#ifdef YP
|
|
|
|
int hostlen = strlen(hostname);
|
|
|
|
#endif
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(hostname != NULL);
|
|
|
|
_DIAGASSERT(e != NULL);
|
|
|
|
|
1993-12-16 08:17:35 +03:00
|
|
|
f = fopen(_PATH_ETHERS, "r");
|
|
|
|
if (f==NULL)
|
|
|
|
return -1;
|
|
|
|
|
1997-11-02 17:21:24 +03:00
|
|
|
while ((p = fgetln(f, &len)) != NULL) {
|
|
|
|
if (p[len - 1] != '\n')
|
|
|
|
continue; /* skip lines w/o \n */
|
|
|
|
p[--len] = '\0';
|
1993-12-16 08:17:35 +03:00
|
|
|
#ifdef YP
|
|
|
|
/* A + in the file means try YP now. */
|
1997-11-02 17:21:24 +03:00
|
|
|
if (len == 1 && *p == '+') {
|
1993-12-16 08:17:35 +03:00
|
|
|
char *ypbuf, *ypdom;
|
|
|
|
int ypbuflen;
|
|
|
|
|
|
|
|
if (yp_get_default_domain(&ypdom))
|
|
|
|
continue;
|
|
|
|
if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
|
|
|
|
&ypbuf, &ypbuflen))
|
|
|
|
continue;
|
|
|
|
if (ether_line(ypbuf, e, try) == 0) {
|
|
|
|
free(ypbuf);
|
|
|
|
(void)fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free(ypbuf);
|
1993-12-16 22:19:14 +03:00
|
|
|
continue;
|
1993-12-16 08:17:35 +03:00
|
|
|
}
|
|
|
|
#endif
|
1997-11-02 17:21:24 +03:00
|
|
|
if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
|
1993-12-16 08:17:35 +03:00
|
|
|
(void)fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)fclose(f);
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-01-17 11:23:26 +03:00
|
|
|
int
|
1993-12-16 08:17:35 +03:00
|
|
|
ether_line(l, e, hostname)
|
1997-11-02 17:21:24 +03:00
|
|
|
const char *l;
|
1993-12-16 08:17:35 +03:00
|
|
|
struct ether_addr *e;
|
|
|
|
char *hostname;
|
|
|
|
{
|
|
|
|
u_int i[6];
|
1997-11-02 17:21:24 +03:00
|
|
|
|
2000-10-04 18:46:23 +04:00
|
|
|
#define S2(arg) #arg
|
|
|
|
#define S1(arg) S2(arg)
|
|
|
|
const static char fmt[] = " %x:%x:%x:%x:%x:%x"
|
|
|
|
" %" S1(MAXHOSTNAMELEN) "s\n";
|
|
|
|
#undef S2
|
|
|
|
#undef S1
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(l != NULL);
|
|
|
|
_DIAGASSERT(e != NULL);
|
|
|
|
_DIAGASSERT(hostname != NULL);
|
|
|
|
|
2000-10-04 18:46:23 +04:00
|
|
|
if (sscanf(l, fmt,
|
1997-11-02 17:21:24 +03:00
|
|
|
&i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
|
1993-12-16 08:17:35 +03:00
|
|
|
e->ether_addr_octet[0] = (u_char)i[0];
|
|
|
|
e->ether_addr_octet[1] = (u_char)i[1];
|
|
|
|
e->ether_addr_octet[2] = (u_char)i[2];
|
|
|
|
e->ether_addr_octet[3] = (u_char)i[3];
|
|
|
|
e->ether_addr_octet[4] = (u_char)i[4];
|
|
|
|
e->ether_addr_octet[5] = (u_char)i[5];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|