ac55411af05cdb636111c323110b8ac06febec5c
[musl] / src / network / ether.c
1 #include <stdlib.h>
2 #include <netinet/ether.h>
3 #include <stdio.h>
4
5 struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a)
6 {
7         struct ether_addr a;
8         char *y;
9         for (int ii = 0; ii < 6; ii++) {
10                 unsigned long int n;
11                 if (ii != 0) {
12                         if (x[0] != ':') return 0; /* bad format */
13                         else x++;
14                 }
15                 n = strtoul (x, &y, 16);
16                 x = y;
17                 if (n > 0xFF) return 0; /* bad byte */
18                 a.ether_addr_octet[ii] = n;
19         }
20         if (x[0] != 0) return 0; /* bad format */
21         *p_a = a;
22         return p_a;
23 }
24
25 struct ether_addr *ether_aton (const char *x)
26 {
27         static struct ether_addr a;
28         return ether_aton_r (x, &a);
29 }
30
31 char *ether_ntoa_r (const struct ether_addr *p_a, char *x) {
32         char *y;
33         y = x;
34         for (int ii = 0; ii < 6; ii++) {
35                 x += sprintf (x, ii == 0 ? "%.2X" : ":%.2X", p_a->ether_addr_octet[ii]);
36         }
37         return y;
38 }
39
40 char *ether_ntoa (const struct ether_addr *p_a) {
41         static char x[18];
42         return ether_ntoa_r (p_a, x);
43 }