xref: /dpdk/lib/cmdline/cmdline_parse_etheraddr.c (revision 99a2dd955fba6e4cc23b77d590a033650ced9c45)
1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation.
3*99a2dd95SBruce Richardson  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4*99a2dd95SBruce Richardson  * All rights reserved.
5*99a2dd95SBruce Richardson  */
6*99a2dd95SBruce Richardson 
7*99a2dd95SBruce Richardson #include <stdio.h>
8*99a2dd95SBruce Richardson #include <string.h>
9*99a2dd95SBruce Richardson 
10*99a2dd95SBruce Richardson #include <rte_string_fns.h>
11*99a2dd95SBruce Richardson #include <rte_ether.h>
12*99a2dd95SBruce Richardson 
13*99a2dd95SBruce Richardson #include "cmdline_parse.h"
14*99a2dd95SBruce Richardson #include "cmdline_parse_etheraddr.h"
15*99a2dd95SBruce Richardson 
16*99a2dd95SBruce Richardson struct cmdline_token_ops cmdline_token_etheraddr_ops = {
17*99a2dd95SBruce Richardson 	.parse = cmdline_parse_etheraddr,
18*99a2dd95SBruce Richardson 	.complete_get_nb = NULL,
19*99a2dd95SBruce Richardson 	.complete_get_elt = NULL,
20*99a2dd95SBruce Richardson 	.get_help = cmdline_get_help_etheraddr,
21*99a2dd95SBruce Richardson };
22*99a2dd95SBruce Richardson 
23*99a2dd95SBruce Richardson int
cmdline_parse_etheraddr(__rte_unused cmdline_parse_token_hdr_t * tk,const char * buf,void * res,unsigned ressize)24*99a2dd95SBruce Richardson cmdline_parse_etheraddr(__rte_unused cmdline_parse_token_hdr_t *tk,
25*99a2dd95SBruce Richardson 	const char *buf, void *res, unsigned ressize)
26*99a2dd95SBruce Richardson {
27*99a2dd95SBruce Richardson 	unsigned int token_len = 0;
28*99a2dd95SBruce Richardson 	char ether_str[RTE_ETHER_ADDR_FMT_SIZE];
29*99a2dd95SBruce Richardson 	struct rte_ether_addr tmp;
30*99a2dd95SBruce Richardson 
31*99a2dd95SBruce Richardson 	if (res && ressize < sizeof(tmp))
32*99a2dd95SBruce Richardson 		return -1;
33*99a2dd95SBruce Richardson 
34*99a2dd95SBruce Richardson 	if (!buf || ! *buf)
35*99a2dd95SBruce Richardson 		return -1;
36*99a2dd95SBruce Richardson 
37*99a2dd95SBruce Richardson 	while (!cmdline_isendoftoken(buf[token_len]))
38*99a2dd95SBruce Richardson 		token_len++;
39*99a2dd95SBruce Richardson 
40*99a2dd95SBruce Richardson 	/* if token doesn't match possible string lengths... */
41*99a2dd95SBruce Richardson 	if (token_len >= RTE_ETHER_ADDR_FMT_SIZE)
42*99a2dd95SBruce Richardson 		return -1;
43*99a2dd95SBruce Richardson 
44*99a2dd95SBruce Richardson 	strlcpy(ether_str, buf, token_len + 1);
45*99a2dd95SBruce Richardson 
46*99a2dd95SBruce Richardson 	if (rte_ether_unformat_addr(ether_str, &tmp) < 0)
47*99a2dd95SBruce Richardson 		return -1;
48*99a2dd95SBruce Richardson 
49*99a2dd95SBruce Richardson 	if (res)
50*99a2dd95SBruce Richardson 		memcpy(res, &tmp, sizeof(tmp));
51*99a2dd95SBruce Richardson 	return token_len;
52*99a2dd95SBruce Richardson }
53*99a2dd95SBruce Richardson 
54*99a2dd95SBruce Richardson int
cmdline_get_help_etheraddr(__rte_unused cmdline_parse_token_hdr_t * tk,char * dstbuf,unsigned int size)55*99a2dd95SBruce Richardson cmdline_get_help_etheraddr(__rte_unused cmdline_parse_token_hdr_t *tk,
56*99a2dd95SBruce Richardson 			       char *dstbuf, unsigned int size)
57*99a2dd95SBruce Richardson {
58*99a2dd95SBruce Richardson 	int ret;
59*99a2dd95SBruce Richardson 
60*99a2dd95SBruce Richardson 	ret = snprintf(dstbuf, size, "Ethernet address");
61*99a2dd95SBruce Richardson 	if (ret < 0)
62*99a2dd95SBruce Richardson 		return -1;
63*99a2dd95SBruce Richardson 	return 0;
64*99a2dd95SBruce Richardson }
65