| 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 |
| 6 | |
| 7 | Signed-off-by: Abdoulaye Walsimou Gaye <awg@...toolkit.org> |
| 8 | --- |
| 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 |
| 14 | |
| 15 | diff --git a/include/netinet/ether.h b/include/netinet/ether.h |
| 16 | new file mode 100644 |
| 17 | index 0000000..44c614e |
| 18 | --- /dev/null |
| 19 | +++ b/include/netinet/ether.h |
| 20 | @@ -0,0 +1,10 @@ |
| 21 | +#ifndef _NETINET_ETHER_H |
| 22 | +#define _NETINET_ETHER_H |
| 23 | + |
| 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 *); |
| 29 | + |
| 30 | +#endif /* !_NETINET_ETHER_H */ |
| 31 | diff --git a/src/network/ethers.c b/src/network/ethers.c |
| 32 | new file mode 100644 |
| 33 | index 0000000..8014581 |
| 34 | --- /dev/null |
| 35 | +++ b/src/network/ethers.c |
| 36 | @@ -0,0 +1,180 @@ |
| 37 | +/* Origin NetBSD: src/lib/libc/net/ethers.c */ |
| 38 | + |
| 39 | +/* |
| 40 | + * ethers(3N) a la Sun. |
| 41 | + * |
| 42 | + * Written by Roland McGrath <roland@...b.com> 10/14/93. |
| 43 | + * Public domain. |
| 44 | + * |
| 45 | + * port for musl by Abdoulaye Walsimou GAYE <awg@...toolkit.org> 2012/10/15 |
| 46 | + */ |
| 47 | + |
| 48 | +#define _BSD_SOURCE |
| 49 | +#include <net/ethernet.h> |
| 50 | +#include <netinet/ether.h> |
| 51 | + |
| 52 | +#include <sys/param.h> |
| 53 | +#include <assert.h> |
| 54 | +#include <errno.h> |
| 55 | +#include <paths.h> |
| 56 | +#include <stdio.h> |
| 57 | +#include <stdlib.h> |
| 58 | +#include <string.h> |
| 59 | + |
| 60 | +#ifndef _PATH_ETHERS |
| 61 | +#define _PATH_ETHERS "/etc/ethers" |
| 62 | +#endif |
| 63 | + |
| 64 | +/* |
| 65 | + * ether_ntoa(): |
| 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 |
| 69 | + * each call. |
| 70 | + */ |
| 71 | +char *ether_ntoa(const struct ether_addr *e) |
| 72 | +{ |
| 73 | + static char a[18]; |
| 74 | + |
| 75 | + assert(e != NULL); |
| 76 | + |
| 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]); |
| 81 | + return a; |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + * ether_aton(): |
| 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. |
| 89 | + */ |
| 90 | +struct ether_addr *ether_aton(const char *s) |
| 91 | +{ |
| 92 | + static struct ether_addr n; |
| 93 | + unsigned int i[6]; |
| 94 | + |
| 95 | + assert(s != NULL); |
| 96 | + |
| 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]; |
| 105 | + return &n; |
| 106 | + } |
| 107 | + return NULL; |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | + * ether_ntohost(): |
| 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. |
| 117 | + */ |
| 118 | +int ether_ntohost(char *hostname, const struct ether_addr *e) |
| 119 | +{ |
| 120 | + FILE *f; |
| 121 | + char *p; |
| 122 | + size_t len; |
| 123 | + struct ether_addr try; |
| 124 | + |
| 125 | + assert(hostname != NULL); |
| 126 | + assert(e != NULL); |
| 127 | + |
| 128 | + f = fopen(_PATH_ETHERS, "r"); |
| 129 | + if (f == NULL) |
| 130 | + return -1; |
| 131 | + while ((p = fgetln(f, &len)) != NULL) { |
| 132 | + if (p[len - 1] != '\n') |
| 133 | + continue; /* skip lines w/o \n */ |
| 134 | + p[--len] = '\0'; |
| 135 | + if (ether_line(p, &try, hostname) == 0 && |
| 136 | + memcmp(&try, e, sizeof try) == 0) { |
| 137 | + (void)fclose(f); |
| 138 | + return 0; |
| 139 | + } |
| 140 | + } |
| 141 | + (void)fclose(f); |
| 142 | + errno = ENOENT; |
| 143 | + return -1; |
| 144 | +} |
| 145 | + |
| 146 | +/* |
| 147 | + * ether_hostton(): |
| 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. |
| 153 | + */ |
| 154 | +int ether_hostton(const char *hostname, struct ether_addr *e) |
| 155 | +{ |
| 156 | + FILE *f; |
| 157 | + char *p; |
| 158 | + size_t len; |
| 159 | + char try[MAXHOSTNAMELEN + 1]; |
| 160 | + |
| 161 | + assert(hostname != NULL); |
| 162 | + assert(e != NULL); |
| 163 | + |
| 164 | + f = fopen(_PATH_ETHERS, "r"); |
| 165 | + if (f==NULL) |
| 166 | + return -1; |
| 167 | + |
| 168 | + while ((p = fgetln(f, &len)) != NULL) { |
| 169 | + if (p[len - 1] != '\n') |
| 170 | + continue; /* skip lines w/o \n */ |
| 171 | + p[--len] = '\0'; |
| 172 | + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) { |
| 173 | + (void)fclose(f); |
| 174 | + return 0; |
| 175 | + } |
| 176 | + } |
| 177 | + (void)fclose(f); |
| 178 | + errno = ENOENT; |
| 179 | + return -1; |
| 180 | +} |
| 181 | + |
| 182 | +/* |
| 183 | + * ether_line(): |
| 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. |
| 188 | + */ |
| 189 | +int ether_line(const char *l, struct ether_addr *e, char *hostname) |
| 190 | +{ |
| 191 | + unsigned int i[6]; |
| 192 | + |
| 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"; |
| 197 | +#undef S2 |
| 198 | +#undef S1 |
| 199 | + |
| 200 | + assert(l != NULL); |
| 201 | + assert(e != NULL); |
| 202 | + assert(hostname != NULL); |
| 203 | + |
| 204 | + if (sscanf(l, fmt, |
| 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]; |
| 212 | + return 0; |
| 213 | + } |
| 214 | + errno = EINVAL; |
| 215 | + return -1; |
| 216 | +} |
| 217 | -- |
| 218 | 1.7.9.5 |
| 219 | |
| 220 | |