1 /* $NetBSD: ethers.c,v 1.23 2012/03/20 17:44:18 matt 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.23 2012/03/20 17:44:18 matt 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"
47 ether_ntoa(const struct ether_addr
*e
)
51 _DIAGASSERT(e
!= NULL
);
53 (void) snprintf(a
, sizeof a
, "%02x:%02x:%02x:%02x:%02x:%02x",
54 e
->ether_addr_octet
[0], e
->ether_addr_octet
[1],
55 e
->ether_addr_octet
[2], e
->ether_addr_octet
[3],
56 e
->ether_addr_octet
[4], e
->ether_addr_octet
[5]);
61 ether_aton(const char *s
)
63 static struct ether_addr n
;
66 _DIAGASSERT(s
!= NULL
);
68 if (sscanf(s
, " %x:%x:%x:%x:%x:%x ", &i
[0], &i
[1],
69 &i
[2], &i
[3], &i
[4], &i
[5]) == 6) {
70 n
.ether_addr_octet
[0] = (u_char
)i
[0];
71 n
.ether_addr_octet
[1] = (u_char
)i
[1];
72 n
.ether_addr_octet
[2] = (u_char
)i
[2];
73 n
.ether_addr_octet
[3] = (u_char
)i
[3];
74 n
.ether_addr_octet
[4] = (u_char
)i
[4];
75 n
.ether_addr_octet
[5] = (u_char
)i
[5];
82 ether_ntohost(char *hostname
, const struct ether_addr
*e
)
87 struct ether_addr
try;
89 char trybuf
[sizeof "xx:xx:xx:xx:xx:xx"];
93 _DIAGASSERT(hostname
!= NULL
);
94 _DIAGASSERT(e
!= NULL
);
97 trylen
= snprintf(trybuf
, sizeof trybuf
, "%x:%x:%x:%x:%x:%x",
98 e
->ether_addr_octet
[0], e
->ether_addr_octet
[1],
99 e
->ether_addr_octet
[2], e
->ether_addr_octet
[3],
100 e
->ether_addr_octet
[4], e
->ether_addr_octet
[5]);
103 f
= fopen(_PATH_ETHERS
, "r");
106 while ((p
= fgetln(f
, &len
)) != NULL
) {
107 if (p
[len
- 1] != '\n')
108 continue; /* skip lines w/o \n */
111 /* A + in the file means try YP now. */
112 if (len
== 1 && *p
== '+') {
116 if (yp_get_default_domain(&ypdom
))
118 if (yp_match(ypdom
, "ethers.byaddr", trybuf
,
119 trylen
, &ypbuf
, &ypbuflen
))
121 if (ether_line(ypbuf
, &try, hostname
) == 0) {
130 if (ether_line(p
, &try, hostname
) == 0 &&
131 memcmp(&try, e
, sizeof try) == 0) {
142 ether_hostton(const char *hostname
, struct ether_addr
*e
)
147 char try[MAXHOSTNAMELEN
+ 1];
149 int hostlen
= (int)strlen(hostname
);
152 _DIAGASSERT(hostname
!= NULL
);
153 _DIAGASSERT(e
!= NULL
);
155 f
= fopen(_PATH_ETHERS
, "r");
159 while ((p
= fgetln(f
, &len
)) != NULL
) {
160 if (p
[len
- 1] != '\n')
161 continue; /* skip lines w/o \n */
164 /* A + in the file means try YP now. */
165 if (len
== 1 && *p
== '+') {
169 if (yp_get_default_domain(&ypdom
))
171 if (yp_match(ypdom
, "ethers.byname", hostname
, hostlen
,
174 if (ether_line(ypbuf
, e
, try) == 0) {
183 if (ether_line(p
, e
, try) == 0 && strcmp(hostname
, try) == 0) {
194 ether_line(const char *l
, struct ether_addr
*e
, char *hostname
)
199 #define S1(arg) S2(arg)
200 static const char fmt
[] = " %x:%x:%x:%x:%x:%x"
201 " %" S1(MAXHOSTNAMELEN
) "s\n";
205 _DIAGASSERT(l
!= NULL
);
206 _DIAGASSERT(e
!= NULL
);
207 _DIAGASSERT(hostname
!= NULL
);
210 &i
[0], &i
[1], &i
[2], &i
[3], &i
[4], &i
[5], hostname
) == 7) {
211 e
->ether_addr_octet
[0] = (u_char
)i
[0];
212 e
->ether_addr_octet
[1] = (u_char
)i
[1];
213 e
->ether_addr_octet
[2] = (u_char
)i
[2];
214 e
->ether_addr_octet
[3] = (u_char
)i
[3];
215 e
->ether_addr_octet
[4] = (u_char
)i
[4];
216 e
->ether_addr_octet
[5] = (u_char
)i
[5];