1 Date: Sat, 20 Oct 2012 22:15:44 +0200
2 From: Abdoulaye Walsimou Gaye <awg@...toolkit.org>
3 To: musl@...ts.openwall.com
4 Cc: Abdoulaye Walsimou Gaye <awg@...toolkit.org>
5 Subject: [PATCH 3/4] Import BSD functions defined in <netinet/ether.h> from NetBSD
7 Signed-off-by: Abdoulaye Walsimou Gaye <awg@...toolkit.org>
9 include/netinet/ether.h | 14 ++++
10 src/network/ethers.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++
11 2 files changed, 194 insertions(+)
12 create mode 100644 include/netinet/ether.h
13 create mode 100644 src/network/ethers.c
15 diff --git a/include/netinet/ether.h b/include/netinet/ether.h
17 index 0000000..44c614e
19 +++ b/include/netinet/ether.h
21 +#ifndef _NETINET_ETHER_H
22 +#define _NETINET_ETHER_H
24 +char *ether_ntoa(const struct ether_addr *);
25 +struct ether_addr *ether_aton(const char *);
26 +int ether_ntohost(char *, const struct ether_addr *);
27 +int ether_hostton(const char *, struct ether_addr *);
28 +int ether_line(const char *, struct ether_addr *, char *);
30 +#endif /* !_NETINET_ETHER_H */
31 diff --git a/src/network/ethers.c b/src/network/ethers.c
33 index 0000000..8014581
35 +++ b/src/network/ethers.c
37 +/* Origin NetBSD: src/lib/libc/net/ethers.c */
40 + * ethers(3N) a la Sun.
42 + * Written by Roland McGrath <roland@...b.com> 10/14/93.
45 + * port for musl by Abdoulaye Walsimou GAYE <awg@...toolkit.org> 2012/10/15
49 +#include <net/ethernet.h>
50 +#include <netinet/ether.h>
52 +#include <sys/param.h>
61 +#define _PATH_ETHERS "/etc/ethers"
66 + * This function converts this structure into an ASCII string of the form
67 + * ``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated
68 + * by colons. It returns a pointer to a static buffer that is reused for
71 +char *ether_ntoa(const struct ether_addr *e)
77 + (void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
78 + e->ether_addr_octet[0], e->ether_addr_octet[1],
79 + e->ether_addr_octet[2], e->ether_addr_octet[3],
80 + e->ether_addr_octet[4], e->ether_addr_octet[5]);
86 + * This function converts an ASCII string of the same form and to a structure
87 + * containing the 6 octets of the address. It returns a pointer to a
88 + * static structure that is reused for each call.
90 +struct ether_addr *ether_aton(const char *s)
92 + static struct ether_addr n;
97 + if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
98 + &i[2], &i[3], &i[4], &i[5]) == 6) {
99 + n.ether_addr_octet[0] = (unsigned char)i[0];
100 + n.ether_addr_octet[1] = (unsigned char)i[1];
101 + n.ether_addr_octet[2] = (unsigned char)i[2];
102 + n.ether_addr_octet[3] = (unsigned char)i[3];
103 + n.ether_addr_octet[4] = (unsigned char)i[4];
104 + n.ether_addr_octet[5] = (unsigned char)i[5];
112 + * This function interrogates the data base mapping host names to Ethernet
113 + * addresses, /etc/ethers.
114 + * It looks up the given Ethernet address and writes the associated host name
115 + * into the character buffer passed.
116 + * It returns zero if it finds the requested host name and -1 if not.
118 +int ether_ntohost(char *hostname, const struct ether_addr *e)
123 + struct ether_addr try;
125 + assert(hostname != NULL);
128 + f = fopen(_PATH_ETHERS, "r");
131 + while ((p = fgetln(f, &len)) != NULL) {
132 + if (p[len - 1] != '\n')
133 + continue; /* skip lines w/o \n */
135 + if (ether_line(p, &try, hostname) == 0 &&
136 + memcmp(&try, e, sizeof try) == 0) {
148 + * This function interrogates the data base mapping host names to Ethernet
149 + * addresses, /etc/ethers.
150 + * It looks up the given host name and writes the associated Ethernet address
151 + * into the structure passed.
152 + * It returns zero if it finds the requested address and -1 if not.
154 +int ether_hostton(const char *hostname, struct ether_addr *e)
159 + char try[MAXHOSTNAMELEN + 1];
161 + assert(hostname != NULL);
164 + f = fopen(_PATH_ETHERS, "r");
168 + while ((p = fgetln(f, &len)) != NULL) {
169 + if (p[len - 1] != '\n')
170 + continue; /* skip lines w/o \n */
172 + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
184 + * This function parses a line from the /etc/ethers file and fills in the passed
185 + * ``struct ether_addr'' and character buffer with the Ethernet address and host
186 + * name on the line.
187 + * It returns zero if the line was successfully parsed and -1 if not.
189 +int ether_line(const char *l, struct ether_addr *e, char *hostname)
193 +#define S2(arg) #arg
194 +#define S1(arg) S2(arg)
195 + static const char fmt[] = " %x:%x:%x:%x:%x:%x"
196 + " %" S1(MAXHOSTNAMELEN) "s\n";
202 + assert(hostname != NULL);
205 + &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
206 + e->ether_addr_octet[0] = (unsigned char)i[0];
207 + e->ether_addr_octet[1] = (unsigned char)i[1];
208 + e->ether_addr_octet[2] = (unsigned char)i[2];
209 + e->ether_addr_octet[3] = (unsigned char)i[3];
210 + e->ether_addr_octet[4] = (unsigned char)i[4];
211 + e->ether_addr_octet[5] = (unsigned char)i[5];