001-bsd_ether_h.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. Date: Sat, 20 Oct 2012 22:15:44 +0200
  2. From: Abdoulaye Walsimou Gaye <[email protected]>
  3. To: [email protected]
  4. Cc: Abdoulaye Walsimou Gaye <[email protected]>
  5. Subject: [PATCH 3/4] Import BSD functions defined in <netinet/ether.h> from NetBSD
  6. Signed-off-by: Abdoulaye Walsimou Gaye <[email protected]>
  7. ---
  8. include/netinet/ether.h | 14 ++++
  9. src/network/ethers.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++
  10. 2 files changed, 194 insertions(+)
  11. create mode 100644 include/netinet/ether.h
  12. create mode 100644 src/network/ethers.c
  13. diff --git a/include/netinet/ether.h b/include/netinet/ether.h
  14. new file mode 100644
  15. index 0000000..44c614e
  16. --- /dev/null
  17. +++ b/include/netinet/ether.h
  18. @@ -0,0 +1,10 @@
  19. +#ifndef _NETINET_ETHER_H
  20. +#define _NETINET_ETHER_H
  21. +
  22. +char *ether_ntoa(const struct ether_addr *);
  23. +struct ether_addr *ether_aton(const char *);
  24. +int ether_ntohost(char *, const struct ether_addr *);
  25. +int ether_hostton(const char *, struct ether_addr *);
  26. +int ether_line(const char *, struct ether_addr *, char *);
  27. +
  28. +#endif /* !_NETINET_ETHER_H */
  29. diff --git a/src/network/ethers.c b/src/network/ethers.c
  30. new file mode 100644
  31. index 0000000..8014581
  32. --- /dev/null
  33. +++ b/src/network/ethers.c
  34. @@ -0,0 +1,180 @@
  35. +/* Origin NetBSD: src/lib/libc/net/ethers.c */
  36. +
  37. +/*
  38. + * ethers(3N) a la Sun.
  39. + *
  40. + * Written by Roland McGrath <[email protected]> 10/14/93.
  41. + * Public domain.
  42. + *
  43. + * port for musl by Abdoulaye Walsimou GAYE <[email protected]> 2012/10/15
  44. + */
  45. +
  46. +#define _BSD_SOURCE
  47. +#include <net/ethernet.h>
  48. +#include <netinet/ether.h>
  49. +
  50. +#include <sys/param.h>
  51. +#include <assert.h>
  52. +#include <errno.h>
  53. +#include <paths.h>
  54. +#include <stdio.h>
  55. +#include <stdlib.h>
  56. +#include <string.h>
  57. +
  58. +#ifndef _PATH_ETHERS
  59. +#define _PATH_ETHERS "/etc/ethers"
  60. +#endif
  61. +
  62. +/*
  63. + * ether_ntoa():
  64. + * This function converts this structure into an ASCII string of the form
  65. + * ``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated
  66. + * by colons. It returns a pointer to a static buffer that is reused for
  67. + * each call.
  68. + */
  69. +char *ether_ntoa(const struct ether_addr *e)
  70. +{
  71. + static char a[18];
  72. +
  73. + assert(e != NULL);
  74. +
  75. + (void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
  76. + e->ether_addr_octet[0], e->ether_addr_octet[1],
  77. + e->ether_addr_octet[2], e->ether_addr_octet[3],
  78. + e->ether_addr_octet[4], e->ether_addr_octet[5]);
  79. + return a;
  80. +}
  81. +
  82. +/*
  83. + * ether_aton():
  84. + * This function converts an ASCII string of the same form and to a structure
  85. + * containing the 6 octets of the address. It returns a pointer to a
  86. + * static structure that is reused for each call.
  87. + */
  88. +struct ether_addr *ether_aton(const char *s)
  89. +{
  90. + static struct ether_addr n;
  91. + unsigned int i[6];
  92. +
  93. + assert(s != NULL);
  94. +
  95. + if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
  96. + &i[2], &i[3], &i[4], &i[5]) == 6) {
  97. + n.ether_addr_octet[0] = (unsigned char)i[0];
  98. + n.ether_addr_octet[1] = (unsigned char)i[1];
  99. + n.ether_addr_octet[2] = (unsigned char)i[2];
  100. + n.ether_addr_octet[3] = (unsigned char)i[3];
  101. + n.ether_addr_octet[4] = (unsigned char)i[4];
  102. + n.ether_addr_octet[5] = (unsigned char)i[5];
  103. + return &n;
  104. + }
  105. + return NULL;
  106. +}
  107. +
  108. +/*
  109. + * ether_ntohost():
  110. + * This function interrogates the data base mapping host names to Ethernet
  111. + * addresses, /etc/ethers.
  112. + * It looks up the given Ethernet address and writes the associated host name
  113. + * into the character buffer passed.
  114. + * It returns zero if it finds the requested host name and -1 if not.
  115. + */
  116. +int ether_ntohost(char *hostname, const struct ether_addr *e)
  117. +{
  118. + FILE *f;
  119. + char *p;
  120. + size_t len;
  121. + struct ether_addr try;
  122. +
  123. + assert(hostname != NULL);
  124. + assert(e != NULL);
  125. +
  126. + f = fopen(_PATH_ETHERS, "r");
  127. + if (f == NULL)
  128. + return -1;
  129. + while ((p = fgetln(f, &len)) != NULL) {
  130. + if (p[len - 1] != '\n')
  131. + continue; /* skip lines w/o \n */
  132. + p[--len] = '\0';
  133. + if (ether_line(p, &try, hostname) == 0 &&
  134. + memcmp(&try, e, sizeof try) == 0) {
  135. + (void)fclose(f);
  136. + return 0;
  137. + }
  138. + }
  139. + (void)fclose(f);
  140. + errno = ENOENT;
  141. + return -1;
  142. +}
  143. +
  144. +/*
  145. + * ether_hostton():
  146. + * This function interrogates the data base mapping host names to Ethernet
  147. + * addresses, /etc/ethers.
  148. + * It looks up the given host name and writes the associated Ethernet address
  149. + * into the structure passed.
  150. + * It returns zero if it finds the requested address and -1 if not.
  151. + */
  152. +int ether_hostton(const char *hostname, struct ether_addr *e)
  153. +{
  154. + FILE *f;
  155. + char *p;
  156. + size_t len;
  157. + char try[MAXHOSTNAMELEN + 1];
  158. +
  159. + assert(hostname != NULL);
  160. + assert(e != NULL);
  161. +
  162. + f = fopen(_PATH_ETHERS, "r");
  163. + if (f==NULL)
  164. + return -1;
  165. +
  166. + while ((p = fgetln(f, &len)) != NULL) {
  167. + if (p[len - 1] != '\n')
  168. + continue; /* skip lines w/o \n */
  169. + p[--len] = '\0';
  170. + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
  171. + (void)fclose(f);
  172. + return 0;
  173. + }
  174. + }
  175. + (void)fclose(f);
  176. + errno = ENOENT;
  177. + return -1;
  178. +}
  179. +
  180. +/*
  181. + * ether_line():
  182. + * This function parses a line from the /etc/ethers file and fills in the passed
  183. + * ``struct ether_addr'' and character buffer with the Ethernet address and host
  184. + * name on the line.
  185. + * It returns zero if the line was successfully parsed and -1 if not.
  186. + */
  187. +int ether_line(const char *l, struct ether_addr *e, char *hostname)
  188. +{
  189. + unsigned int i[6];
  190. +
  191. +#define S2(arg) #arg
  192. +#define S1(arg) S2(arg)
  193. + static const char fmt[] = " %x:%x:%x:%x:%x:%x"
  194. + " %" S1(MAXHOSTNAMELEN) "s\n";
  195. +#undef S2
  196. +#undef S1
  197. +
  198. + assert(l != NULL);
  199. + assert(e != NULL);
  200. + assert(hostname != NULL);
  201. +
  202. + if (sscanf(l, fmt,
  203. + &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
  204. + e->ether_addr_octet[0] = (unsigned char)i[0];
  205. + e->ether_addr_octet[1] = (unsigned char)i[1];
  206. + e->ether_addr_octet[2] = (unsigned char)i[2];
  207. + e->ether_addr_octet[3] = (unsigned char)i[3];
  208. + e->ether_addr_octet[4] = (unsigned char)i[4];
  209. + e->ether_addr_octet[5] = (unsigned char)i[5];
  210. + return 0;
  211. + }
  212. + errno = EINVAL;
  213. + return -1;
  214. +}
  215. --
  216. 1.7.9.5