1 /* $NetBSD: ethers.c,v 1.20 2002/09/16 19:25:33 tron Exp $ */
6 * Written by Roland McGrath <roland@frob.com> 10/14/93.
10 #include <sys/cdefs.h>
11 #if defined(LIBC_SCCS) && !defined(lint)
12 __RCSID("$NetBSD: ethers.c,v 1.20 2002/09/16 19:25:33 tron Exp $");
13 #endif /* LIBC_SCCS and not lint */
15 #include "namespace.h"
16 #include <sys/param.h>
17 #include <sys/socket.h>
20 #include <net/if_ether.h>
21 #include <netinet/in.h>
31 #include <rpcsvc/ypclnt.h>
35 __weak_alias(ether_aton
,_ether_aton
)
36 __weak_alias(ether_hostton
,_ether_hostton
)
37 __weak_alias(ether_line
,_ether_line
)
38 __weak_alias(ether_ntoa
,_ether_ntoa
)
39 __weak_alias(ether_ntohost
,_ether_ntohost
)
43 #define _PATH_ETHERS "/etc/ethers"
48 const struct ether_addr
*e
;
52 _DIAGASSERT(e
!= NULL
);
54 (void) snprintf(a
, sizeof a
, "%02x:%02x:%02x:%02x:%02x:%02x",
55 e
->ether_addr_octet
[0], e
->ether_addr_octet
[1],
56 e
->ether_addr_octet
[2], e
->ether_addr_octet
[3],
57 e
->ether_addr_octet
[4], e
->ether_addr_octet
[5]);
65 static struct ether_addr n
;
68 _DIAGASSERT(s
!= NULL
);
70 if (sscanf(s
, " %x:%x:%x:%x:%x:%x ", &i
[0], &i
[1],
71 &i
[2], &i
[3], &i
[4], &i
[5]) == 6) {
72 n
.ether_addr_octet
[0] = (u_char
)i
[0];
73 n
.ether_addr_octet
[1] = (u_char
)i
[1];
74 n
.ether_addr_octet
[2] = (u_char
)i
[2];
75 n
.ether_addr_octet
[3] = (u_char
)i
[3];
76 n
.ether_addr_octet
[4] = (u_char
)i
[4];
77 n
.ether_addr_octet
[5] = (u_char
)i
[5];
84 ether_ntohost(hostname
, e
)
86 const struct ether_addr
*e
;
91 struct ether_addr
try;
93 char trybuf
[sizeof "xx:xx:xx:xx:xx:xx"];
97 _DIAGASSERT(hostname
!= NULL
);
98 _DIAGASSERT(e
!= NULL
);
101 trylen
= snprintf(trybuf
, sizeof trybuf
, "%x:%x:%x:%x:%x:%x",
102 e
->ether_addr_octet
[0], e
->ether_addr_octet
[1],
103 e
->ether_addr_octet
[2], e
->ether_addr_octet
[3],
104 e
->ether_addr_octet
[4], e
->ether_addr_octet
[5]);
107 f
= fopen(_PATH_ETHERS
, "r");
110 while ((p
= fgetln(f
, &len
)) != NULL
) {
111 if (p
[len
- 1] != '\n')
112 continue; /* skip lines w/o \n */
115 /* A + in the file means try YP now. */
116 if (len
== 1 && *p
== '+') {
120 if (yp_get_default_domain(&ypdom
))
122 if (yp_match(ypdom
, "ethers.byaddr", trybuf
,
123 trylen
, &ypbuf
, &ypbuflen
))
125 if (ether_line(ypbuf
, &try, hostname
) == 0) {
134 if (ether_line(p
, &try, hostname
) == 0 &&
135 memcmp(&try, e
, sizeof try) == 0) {
146 ether_hostton(hostname
, e
)
147 const char *hostname
;
148 struct ether_addr
*e
;
153 char try[MAXHOSTNAMELEN
+ 1];
155 int hostlen
= strlen(hostname
);
158 _DIAGASSERT(hostname
!= NULL
);
159 _DIAGASSERT(e
!= NULL
);
161 f
= fopen(_PATH_ETHERS
, "r");
165 while ((p
= fgetln(f
, &len
)) != NULL
) {
166 if (p
[len
- 1] != '\n')
167 continue; /* skip lines w/o \n */
170 /* A + in the file means try YP now. */
171 if (len
== 1 && *p
== '+') {
175 if (yp_get_default_domain(&ypdom
))
177 if (yp_match(ypdom
, "ethers.byname", hostname
, hostlen
,
180 if (ether_line(ypbuf
, e
, try) == 0) {
189 if (ether_line(p
, e
, try) == 0 && strcmp(hostname
, try) == 0) {
200 ether_line(l
, e
, hostname
)
202 struct ether_addr
*e
;
208 #define S1(arg) S2(arg)
209 static const char fmt
[] = " %x:%x:%x:%x:%x:%x"
210 " %" S1(MAXHOSTNAMELEN
) "s\n";
214 _DIAGASSERT(l
!= NULL
);
215 _DIAGASSERT(e
!= NULL
);
216 _DIAGASSERT(hostname
!= NULL
);
219 &i
[0], &i
[1], &i
[2], &i
[3], &i
[4], &i
[5], hostname
) == 7) {
220 e
->ether_addr_octet
[0] = (u_char
)i
[0];
221 e
->ether_addr_octet
[1] = (u_char
)i
[1];
222 e
->ether_addr_octet
[2] = (u_char
)i
[2];
223 e
->ether_addr_octet
[3] = (u_char
)i
[3];
224 e
->ether_addr_octet
[4] = (u_char
)i
[4];
225 e
->ether_addr_octet
[5] = (u_char
)i
[5];