2 +++ b/include/netinet/ether.h
4 +#ifndef _NETINET_ETHER_H
5 +#define _NETINET_ETHER_H
7 +char *ether_ntoa(const struct ether_addr *);
8 +struct ether_addr *ether_aton(const char *);
9 +int ether_ntohost(char *, const struct ether_addr *);
10 +int ether_hostton(const char *, struct ether_addr *);
11 +int ether_line(const char *, struct ether_addr *, char *);
13 +#endif /* !_NETINET_ETHER_H */
15 +++ b/src/network/ethers.c
17 +/* Origin NetBSD: src/lib/libc/net/ethers.c */
20 + * ethers(3N) a la Sun.
22 + * Written by Roland McGrath <roland@...b.com> 10/14/93.
25 + * port for musl by Abdoulaye Walsimou GAYE <awg@...toolkit.org> 2012/10/15
29 +#include <net/ethernet.h>
30 +#include <netinet/ether.h>
32 +#include <sys/param.h>
41 +#define _PATH_ETHERS "/etc/ethers"
46 + * This function converts this structure into an ASCII string of the form
47 + * ``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated
48 + * by colons. It returns a pointer to a static buffer that is reused for
51 +char *ether_ntoa(const struct ether_addr *e)
57 + (void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
58 + e->ether_addr_octet[0], e->ether_addr_octet[1],
59 + e->ether_addr_octet[2], e->ether_addr_octet[3],
60 + e->ether_addr_octet[4], e->ether_addr_octet[5]);
66 + * This function converts an ASCII string of the same form and to a structure
67 + * containing the 6 octets of the address. It returns a pointer to a
68 + * static structure that is reused for each call.
70 +struct ether_addr *ether_aton(const char *s)
72 + static struct ether_addr n;
77 + if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
78 + &i[2], &i[3], &i[4], &i[5]) == 6) {
79 + n.ether_addr_octet[0] = (unsigned char)i[0];
80 + n.ether_addr_octet[1] = (unsigned char)i[1];
81 + n.ether_addr_octet[2] = (unsigned char)i[2];
82 + n.ether_addr_octet[3] = (unsigned char)i[3];
83 + n.ether_addr_octet[4] = (unsigned char)i[4];
84 + n.ether_addr_octet[5] = (unsigned char)i[5];
92 + * This function interrogates the data base mapping host names to Ethernet
93 + * addresses, /etc/ethers.
94 + * It looks up the given Ethernet address and writes the associated host name
95 + * into the character buffer passed.
96 + * It returns zero if it finds the requested host name and -1 if not.
98 +int ether_ntohost(char *hostname, const struct ether_addr *e)
103 + struct ether_addr try;
105 + assert(hostname != NULL);
108 + f = fopen(_PATH_ETHERS, "r");
111 + while ((p = fgetln(f, &len)) != NULL) {
112 + if (p[len - 1] != '\n')
113 + continue; /* skip lines w/o \n */
115 + if (ether_line(p, &try, hostname) == 0 &&
116 + memcmp(&try, e, sizeof try) == 0) {
128 + * This function interrogates the data base mapping host names to Ethernet
129 + * addresses, /etc/ethers.
130 + * It looks up the given host name and writes the associated Ethernet address
131 + * into the structure passed.
132 + * It returns zero if it finds the requested address and -1 if not.
134 +int ether_hostton(const char *hostname, struct ether_addr *e)
139 + char try[MAXHOSTNAMELEN + 1];
141 + assert(hostname != NULL);
144 + f = fopen(_PATH_ETHERS, "r");
148 + while ((p = fgetln(f, &len)) != NULL) {
149 + if (p[len - 1] != '\n')
150 + continue; /* skip lines w/o \n */
152 + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
164 + * This function parses a line from the /etc/ethers file and fills in the passed
165 + * ``struct ether_addr'' and character buffer with the Ethernet address and host
166 + * name on the line.
167 + * It returns zero if the line was successfully parsed and -1 if not.
169 +int ether_line(const char *l, struct ether_addr *e, char *hostname)
173 +#define S2(arg) #arg
174 +#define S1(arg) S2(arg)
175 + static const char fmt[] = " %x:%x:%x:%x:%x:%x"
176 + " %" S1(MAXHOSTNAMELEN) "s\n";
182 + assert(hostname != NULL);
185 + &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
186 + e->ether_addr_octet[0] = (unsigned char)i[0];
187 + e->ether_addr_octet[1] = (unsigned char)i[1];
188 + e->ether_addr_octet[2] = (unsigned char)i[2];
189 + e->ether_addr_octet[3] = (unsigned char)i[3];
190 + e->ether_addr_octet[4] = (unsigned char)i[4];
191 + e->ether_addr_octet[5] = (unsigned char)i[5];