blob: f0eda37c063d5c06591fe829088e840a69a47899 [file] [log] [blame]
William A. Kennington III7d6fa422021-02-08 17:04:02 -08001#include <inttypes.h>
2#include <net/ethernet.h>
3#include <stdio.h>
4
5#define ETH_STRLEN (ETH_ALEN * 3)
6#define HEX_OUT "%02" PRIx8
7#define ETH_OUT \
8 HEX_OUT ":" HEX_OUT ":" HEX_OUT ":" HEX_OUT ":" HEX_OUT ":" HEX_OUT
9#define HEX_IN "%2" SCNx8
10#define ETH_IN HEX_IN ":" HEX_IN ":" HEX_IN ":" HEX_IN ":" HEX_IN ":" HEX_IN
11
12int to_ether_addr(const char* str, struct ether_addr* ret)
13{
14 char sentinel;
15 return sscanf(str, ETH_IN "%c", &ret->ether_addr_octet[0],
16 &ret->ether_addr_octet[1], &ret->ether_addr_octet[2],
17 &ret->ether_addr_octet[3], &ret->ether_addr_octet[4],
18 &ret->ether_addr_octet[5], &sentinel) != ETH_ALEN;
19}
20
21void from_ether_addr(const struct ether_addr* addr, char* ret)
22{
23 sprintf(ret, ETH_OUT, addr->ether_addr_octet[0], addr->ether_addr_octet[1],
24 addr->ether_addr_octet[2], addr->ether_addr_octet[3],
25 addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
26}
27
28int main(int argc, char* argv[])
29{
30 if (argc < 1)
31 {
32 return 1;
33 }
34 if (argc != 2)
35 {
36 fprintf(stderr, "Usage: %s <mac address>\n", argv[0]);
37 return 1;
38 }
39
40 struct ether_addr addr;
41 if (to_ether_addr(argv[1], &addr) != 0)
42 {
43 fprintf(stderr, "Invalid MAC Address: %s\n", argv[1]);
44 return 2;
45 }
46
47 char str[ETH_STRLEN];
48 from_ether_addr(&addr, str);
49 printf("%s\n", str);
50 return 0;
51}