1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
14 #include <sys/types.h>
15 #include <net/ethernet.h>
17 #include <rte_string_fns.h>
19 #include "cmdline_parse.h"
20 #include "cmdline_parse_etheraddr.h"
22 struct cmdline_token_ops cmdline_token_etheraddr_ops = {
23 .parse = cmdline_parse_etheraddr,
24 .complete_get_nb = NULL,
25 .complete_get_elt = NULL,
26 .get_help = cmdline_get_help_etheraddr,
29 /* the format can be either XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX */
30 #define ETHER_ADDRSTRLENLONG 18
31 #define ETHER_ADDRSTRLENSHORT 15
34 #define ea_oct ether_addr_octet
40 static struct ether_addr *
41 my_ether_aton(const char *a)
45 unsigned long o[ETHER_ADDR_LEN];
46 static struct ether_addr ether_addr;
51 o[i] = strtoul(a, &end, 16);
52 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
55 } while (++i != sizeof (o) / sizeof (o[0]) && end[0] != 0);
57 /* Junk at the end of line */
61 /* Support the format XX:XX:XX:XX:XX:XX */
62 if (i == ETHER_ADDR_LEN) {
66 ether_addr.ea_oct[i] = (uint8_t)o[i];
68 /* Support the format XXXX:XXXX:XXXX */
69 } else if (i == ETHER_ADDR_LEN / 2) {
71 if (o[i] > UINT16_MAX)
73 ether_addr.ea_oct[i * 2] = (uint8_t)(o[i] >> 8);
74 ether_addr.ea_oct[i * 2 + 1] = (uint8_t)(o[i] & 0xff);
80 return (struct ether_addr *)ðer_addr;
84 cmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
85 const char *buf, void *res, unsigned ressize)
87 unsigned int token_len = 0;
88 char ether_str[ETHER_ADDRSTRLENLONG+1];
89 struct ether_addr *tmp;
91 if (res && ressize < sizeof(struct ether_addr))
97 while (!cmdline_isendoftoken(buf[token_len]))
100 /* if token doesn't match possible string lengths... */
101 if ((token_len != ETHER_ADDRSTRLENLONG - 1) &&
102 (token_len != ETHER_ADDRSTRLENSHORT - 1))
105 strlcpy(ether_str, buf, token_len + 1);
107 tmp = my_ether_aton(ether_str);
111 memcpy(res, tmp, sizeof(struct ether_addr));
116 cmdline_get_help_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
117 char *dstbuf, unsigned int size)
121 ret = snprintf(dstbuf, size, "Ethernet address");