110-bsd_ether_h.patch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. --- /dev/null
  2. +++ b/include/netinet/ether.h
  3. @@ -0,0 +1,10 @@
  4. +#ifndef _NETINET_ETHER_H
  5. +#define _NETINET_ETHER_H
  6. +
  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 *);
  12. +
  13. +#endif /* !_NETINET_ETHER_H */
  14. --- /dev/null
  15. +++ b/src/network/ethers.c
  16. @@ -0,0 +1,180 @@
  17. +/* Origin NetBSD: src/lib/libc/net/ethers.c */
  18. +
  19. +/*
  20. + * ethers(3N) a la Sun.
  21. + *
  22. + * Written by Roland McGrath <[email protected]> 10/14/93.
  23. + * Public domain.
  24. + *
  25. + * port for musl by Abdoulaye Walsimou GAYE <[email protected]> 2012/10/15
  26. + */
  27. +
  28. +#define _BSD_SOURCE
  29. +#include <net/ethernet.h>
  30. +#include <netinet/ether.h>
  31. +
  32. +#include <sys/param.h>
  33. +#include <assert.h>
  34. +#include <errno.h>
  35. +#include <paths.h>
  36. +#include <stdio.h>
  37. +#include <stdlib.h>
  38. +#include <string.h>
  39. +
  40. +#ifndef _PATH_ETHERS
  41. +#define _PATH_ETHERS "/etc/ethers"
  42. +#endif
  43. +
  44. +/*
  45. + * ether_ntoa():
  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
  49. + * each call.
  50. + */
  51. +char *ether_ntoa(const struct ether_addr *e)
  52. +{
  53. + static char a[18];
  54. +
  55. + assert(e != NULL);
  56. +
  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]);
  61. + return a;
  62. +}
  63. +
  64. +/*
  65. + * ether_aton():
  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.
  69. + */
  70. +struct ether_addr *ether_aton(const char *s)
  71. +{
  72. + static struct ether_addr n;
  73. + unsigned int i[6];
  74. +
  75. + assert(s != NULL);
  76. +
  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];
  85. + return &n;
  86. + }
  87. + return NULL;
  88. +}
  89. +
  90. +/*
  91. + * ether_ntohost():
  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.
  97. + */
  98. +int ether_ntohost(char *hostname, const struct ether_addr *e)
  99. +{
  100. + FILE *f;
  101. + char *p;
  102. + size_t len;
  103. + struct ether_addr try;
  104. +
  105. + assert(hostname != NULL);
  106. + assert(e != NULL);
  107. +
  108. + f = fopen(_PATH_ETHERS, "r");
  109. + if (f == NULL)
  110. + return -1;
  111. + while ((p = fgetln(f, &len)) != NULL) {
  112. + if (p[len - 1] != '\n')
  113. + continue; /* skip lines w/o \n */
  114. + p[--len] = '\0';
  115. + if (ether_line(p, &try, hostname) == 0 &&
  116. + memcmp(&try, e, sizeof try) == 0) {
  117. + (void)fclose(f);
  118. + return 0;
  119. + }
  120. + }
  121. + (void)fclose(f);
  122. + errno = ENOENT;
  123. + return -1;
  124. +}
  125. +
  126. +/*
  127. + * ether_hostton():
  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.
  133. + */
  134. +int ether_hostton(const char *hostname, struct ether_addr *e)
  135. +{
  136. + FILE *f;
  137. + char *p;
  138. + size_t len;
  139. + char try[MAXHOSTNAMELEN + 1];
  140. +
  141. + assert(hostname != NULL);
  142. + assert(e != NULL);
  143. +
  144. + f = fopen(_PATH_ETHERS, "r");
  145. + if (f==NULL)
  146. + return -1;
  147. +
  148. + while ((p = fgetln(f, &len)) != NULL) {
  149. + if (p[len - 1] != '\n')
  150. + continue; /* skip lines w/o \n */
  151. + p[--len] = '\0';
  152. + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
  153. + (void)fclose(f);
  154. + return 0;
  155. + }
  156. + }
  157. + (void)fclose(f);
  158. + errno = ENOENT;
  159. + return -1;
  160. +}
  161. +
  162. +/*
  163. + * ether_line():
  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.
  168. + */
  169. +int ether_line(const char *l, struct ether_addr *e, char *hostname)
  170. +{
  171. + unsigned int i[6];
  172. +
  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";
  177. +#undef S2
  178. +#undef S1
  179. +
  180. + assert(l != NULL);
  181. + assert(e != NULL);
  182. + assert(hostname != NULL);
  183. +
  184. + if (sscanf(l, fmt,
  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];
  192. + return 0;
  193. + }
  194. + errno = EINVAL;
  195. + return -1;
  196. +}