1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation. 3 * Copyright (c) 2009, Olivier MATZ <[email protected]> 4 * All rights reserved. 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <stdarg.h> 10 #include <errno.h> 11 #include <inttypes.h> 12 #include <ctype.h> 13 #include <string.h> 14 #include <sys/types.h> 15 16 #include <rte_string_fns.h> 17 #include <rte_ether.h> 18 19 #include "cmdline_parse.h" 20 #include "cmdline_parse_etheraddr.h" 21 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, 27 }; 28 29 int 30 cmdline_parse_etheraddr(__rte_unused cmdline_parse_token_hdr_t *tk, 31 const char *buf, void *res, unsigned ressize) 32 { 33 unsigned int token_len = 0; 34 char ether_str[RTE_ETHER_ADDR_FMT_SIZE]; 35 struct rte_ether_addr tmp; 36 37 if (res && ressize < sizeof(tmp)) 38 return -1; 39 40 if (!buf || ! *buf) 41 return -1; 42 43 while (!cmdline_isendoftoken(buf[token_len])) 44 token_len++; 45 46 /* if token doesn't match possible string lengths... */ 47 if (token_len >= RTE_ETHER_ADDR_FMT_SIZE) 48 return -1; 49 50 strlcpy(ether_str, buf, token_len + 1); 51 52 if (rte_ether_unformat_addr(ether_str, &tmp) < 0) 53 return -1; 54 55 if (res) 56 memcpy(res, &tmp, sizeof(tmp)); 57 return token_len; 58 } 59 60 int 61 cmdline_get_help_etheraddr(__rte_unused cmdline_parse_token_hdr_t *tk, 62 char *dstbuf, unsigned int size) 63 { 64 int ret; 65 66 ret = snprintf(dstbuf, size, "Ethernet address"); 67 if (ret < 0) 68 return -1; 69 return 0; 70 } 71