15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier/*
355129044dSC.J. Collier * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
365129044dSC.J. Collier * All rights reserved.
375129044dSC.J. Collier * Redistribution and use in source and binary forms, with or without
385129044dSC.J. Collier * modification, are permitted provided that the following conditions are met:
395129044dSC.J. Collier *
405129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
415129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
425129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
435129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in the
445129044dSC.J. Collier *       documentation and/or other materials provided with the distribution.
455129044dSC.J. Collier *     * Neither the name of the University of California, Berkeley nor the
465129044dSC.J. Collier *       names of its contributors may be used to endorse or promote products
475129044dSC.J. Collier *       derived from this software without specific prior written permission.
485129044dSC.J. Collier *
495129044dSC.J. Collier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
505129044dSC.J. Collier * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
515129044dSC.J. Collier * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
525129044dSC.J. Collier * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
535129044dSC.J. Collier * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
545129044dSC.J. Collier * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
555129044dSC.J. Collier * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
565129044dSC.J. Collier * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
575129044dSC.J. Collier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
585129044dSC.J. Collier * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
595129044dSC.J. Collier */
605129044dSC.J. Collier
615129044dSC.J. Collier#include <stdio.h>
625129044dSC.J. Collier#include <stdlib.h>
635129044dSC.J. Collier#include <stdarg.h>
645129044dSC.J. Collier#include <errno.h>
655129044dSC.J. Collier#include <inttypes.h>
665129044dSC.J. Collier#include <ctype.h>
675129044dSC.J. Collier#include <string.h>
685129044dSC.J. Collier#include <errno.h>
695129044dSC.J. Collier#include <sys/types.h>
705129044dSC.J. Collier#include <net/ethernet.h>
715129044dSC.J. Collier
725129044dSC.J. Collier#include <rte_string_fns.h>
735129044dSC.J. Collier
745129044dSC.J. Collier#include "cmdline_parse.h"
755129044dSC.J. Collier#include "cmdline_parse_etheraddr.h"
765129044dSC.J. Collier
775129044dSC.J. Collierstruct cmdline_token_ops cmdline_token_etheraddr_ops = {
785129044dSC.J. Collier	.parse = cmdline_parse_etheraddr,
795129044dSC.J. Collier	.complete_get_nb = NULL,
805129044dSC.J. Collier	.complete_get_elt = NULL,
815129044dSC.J. Collier	.get_help = cmdline_get_help_etheraddr,
825129044dSC.J. Collier};
835129044dSC.J. Collier
845129044dSC.J. Collier/* the format can be either XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX */
855129044dSC.J. Collier#define ETHER_ADDRSTRLENLONG 18
865129044dSC.J. Collier#define ETHER_ADDRSTRLENSHORT 15
875129044dSC.J. Collier
885129044dSC.J. Collier#ifdef __linux__
895129044dSC.J. Collier#define ea_oct ether_addr_octet
905129044dSC.J. Collier#else
915129044dSC.J. Collier#define ea_oct octet
925129044dSC.J. Collier#endif
935129044dSC.J. Collier
945129044dSC.J. Collier
955129044dSC.J. Collierstatic struct ether_addr *
965129044dSC.J. Colliermy_ether_aton(const char *a)
975129044dSC.J. Collier{
985129044dSC.J. Collier	int i;
995129044dSC.J. Collier	char *end;
1005129044dSC.J. Collier	unsigned long o[ETHER_ADDR_LEN];
1015129044dSC.J. Collier	static struct ether_addr ether_addr;
1025129044dSC.J. Collier
1035129044dSC.J. Collier	i = 0;
1045129044dSC.J. Collier	do {
1055129044dSC.J. Collier		errno = 0;
1065129044dSC.J. Collier		o[i] = strtoul(a, &end, 16);
1075129044dSC.J. Collier		if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
1085129044dSC.J. Collier			return NULL;
1095129044dSC.J. Collier		a = end + 1;
1105129044dSC.J. Collier	} while (++i != sizeof (o) / sizeof (o[0]) && end[0] != 0);
1115129044dSC.J. Collier
1125129044dSC.J. Collier	/* Junk at the end of line */
1135129044dSC.J. Collier	if (end[0] != 0)
1145129044dSC.J. Collier		return NULL;
1155129044dSC.J. Collier
1165129044dSC.J. Collier	/* Support the format XX:XX:XX:XX:XX:XX */
1175129044dSC.J. Collier	if (i == ETHER_ADDR_LEN) {
1185129044dSC.J. Collier		while (i-- != 0) {
1195129044dSC.J. Collier			if (o[i] > UINT8_MAX)
1205129044dSC.J. Collier				return NULL;
1215129044dSC.J. Collier			ether_addr.ea_oct[i] = (uint8_t)o[i];
1225129044dSC.J. Collier		}
1235129044dSC.J. Collier	/* Support the format XXXX:XXXX:XXXX */
1245129044dSC.J. Collier	} else if (i == ETHER_ADDR_LEN / 2) {
1255129044dSC.J. Collier		while (i-- != 0) {
1265129044dSC.J. Collier			if (o[i] > UINT16_MAX)
1275129044dSC.J. Collier				return NULL;
1285129044dSC.J. Collier			ether_addr.ea_oct[i * 2] = (uint8_t)(o[i] >> 8);
1295129044dSC.J. Collier			ether_addr.ea_oct[i * 2 + 1] = (uint8_t)(o[i] & 0xff);
1305129044dSC.J. Collier		}
1315129044dSC.J. Collier	/* unknown format */
1325129044dSC.J. Collier	} else
1335129044dSC.J. Collier		return NULL;
1345129044dSC.J. Collier
1355129044dSC.J. Collier	return (struct ether_addr *)&ether_addr;
1365129044dSC.J. Collier}
1375129044dSC.J. Collier
1385129044dSC.J. Collierint
1395129044dSC.J. Colliercmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
1405129044dSC.J. Collier	const char *buf, void *res, unsigned ressize)
1415129044dSC.J. Collier{
1425129044dSC.J. Collier	unsigned int token_len = 0;
1435129044dSC.J. Collier	char ether_str[ETHER_ADDRSTRLENLONG+1];
1445129044dSC.J. Collier	struct ether_addr *tmp;
1455129044dSC.J. Collier
1465129044dSC.J. Collier	if (res && ressize < sizeof(struct ether_addr))
1475129044dSC.J. Collier		return -1;
1485129044dSC.J. Collier
1495129044dSC.J. Collier	if (!buf || ! *buf)
1505129044dSC.J. Collier		return -1;
1515129044dSC.J. Collier
1525129044dSC.J. Collier	while (!cmdline_isendoftoken(buf[token_len]))
1535129044dSC.J. Collier		token_len++;
1545129044dSC.J. Collier
1555129044dSC.J. Collier	/* if token doesn't match possible string lengths... */
1565129044dSC.J. Collier	if ((token_len != ETHER_ADDRSTRLENLONG - 1) &&
1575129044dSC.J. Collier			(token_len != ETHER_ADDRSTRLENSHORT - 1))
1585129044dSC.J. Collier		return -1;
1595129044dSC.J. Collier
1605129044dSC.J. Collier	snprintf(ether_str, token_len+1, "%s", buf);
1615129044dSC.J. Collier
1625129044dSC.J. Collier	tmp = my_ether_aton(ether_str);
1635129044dSC.J. Collier	if (tmp == NULL)
1645129044dSC.J. Collier		return -1;
1655129044dSC.J. Collier	if (res)
1665129044dSC.J. Collier		memcpy(res, tmp, sizeof(struct ether_addr));
1675129044dSC.J. Collier	return token_len;
1685129044dSC.J. Collier}
1695129044dSC.J. Collier
1705129044dSC.J. Collierint
1715129044dSC.J. Colliercmdline_get_help_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
1725129044dSC.J. Collier			       char *dstbuf, unsigned int size)
1735129044dSC.J. Collier{
1745129044dSC.J. Collier	int ret;
1755129044dSC.J. Collier
1765129044dSC.J. Collier	ret = snprintf(dstbuf, size, "Ethernet address");
1775129044dSC.J. Collier	if (ret < 0)
1785129044dSC.J. Collier		return -1;
1795129044dSC.J. Collier	return 0;
1805129044dSC.J. Collier}
181