2018-01-29 13:11:20 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* Copyright(c) 2010-2014 Intel Corporation.
|
2012-09-04 12:54:00 +00:00
|
|
|
* Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2018-01-29 10:29:03 +00:00
|
|
|
#include <arpa/inet.h>
|
2012-09-04 12:54:00 +00:00
|
|
|
#include <netinet/in.h>
|
2019-08-12 07:00:54 +00:00
|
|
|
#ifdef __FreeBSD__
|
2014-02-10 11:49:10 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2012-09-04 12:54:00 +00:00
|
|
|
|
|
|
|
#include <rte_string_fns.h>
|
|
|
|
|
|
|
|
#include "cmdline_parse.h"
|
|
|
|
#include "cmdline_parse_ipaddr.h"
|
|
|
|
|
|
|
|
struct cmdline_token_ops cmdline_token_ipaddr_ops = {
|
|
|
|
.parse = cmdline_parse_ipaddr,
|
|
|
|
.complete_get_nb = NULL,
|
|
|
|
.complete_get_elt = NULL,
|
|
|
|
.get_help = cmdline_get_help_ipaddr,
|
|
|
|
};
|
|
|
|
|
2012-12-19 23:00:00 +00:00
|
|
|
#define PREFIXMAX 128
|
|
|
|
#define V4PREFIXMAX 32
|
2012-09-04 12:54:00 +00:00
|
|
|
|
|
|
|
int
|
2014-12-05 14:19:07 +00:00
|
|
|
cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
|
|
|
|
unsigned ressize)
|
2012-09-04 12:54:00 +00:00
|
|
|
{
|
2012-12-19 23:00:00 +00:00
|
|
|
struct cmdline_token_ipaddr *tk2;
|
2012-09-04 12:54:00 +00:00
|
|
|
unsigned int token_len = 0;
|
|
|
|
char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
|
|
|
|
cmdline_ipaddr_t ipaddr;
|
|
|
|
char *prefix, *prefix_end;
|
2012-12-19 23:00:00 +00:00
|
|
|
long prefixlen = 0;
|
2012-09-04 12:54:00 +00:00
|
|
|
|
2014-12-05 14:19:07 +00:00
|
|
|
if (res && ressize < sizeof(cmdline_ipaddr_t))
|
|
|
|
return -1;
|
|
|
|
|
2012-12-19 23:00:00 +00:00
|
|
|
if (!buf || !tk || ! *buf)
|
2012-09-04 12:54:00 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-12-19 23:00:00 +00:00
|
|
|
tk2 = (struct cmdline_token_ipaddr *)tk;
|
|
|
|
|
2012-09-04 12:54:00 +00:00
|
|
|
while (!cmdline_isendoftoken(buf[token_len]))
|
|
|
|
token_len++;
|
|
|
|
|
|
|
|
/* if token is too big... */
|
|
|
|
if (token_len >= INET6_ADDRSTRLEN+4)
|
|
|
|
return -1;
|
|
|
|
|
2018-03-12 11:33:00 +00:00
|
|
|
strlcpy(ip_str, buf, token_len + 1);
|
2012-09-04 12:54:00 +00:00
|
|
|
|
|
|
|
/* convert the network prefix */
|
|
|
|
if (tk2->ipaddr_data.flags & CMDLINE_IPADDR_NETWORK) {
|
|
|
|
prefix = strrchr(ip_str, '/');
|
|
|
|
if (prefix == NULL)
|
|
|
|
return -1;
|
|
|
|
*prefix = '\0';
|
|
|
|
prefix ++;
|
|
|
|
errno = 0;
|
|
|
|
prefixlen = strtol(prefix, &prefix_end, 10);
|
2012-12-19 23:00:00 +00:00
|
|
|
if (errno || (*prefix_end != '\0')
|
|
|
|
|| prefixlen < 0 || prefixlen > PREFIXMAX)
|
2012-09-04 12:54:00 +00:00
|
|
|
return -1;
|
|
|
|
ipaddr.prefixlen = prefixlen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ipaddr.prefixlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert the IP addr */
|
|
|
|
if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
|
2018-01-29 10:29:03 +00:00
|
|
|
inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1 &&
|
2012-12-19 23:00:00 +00:00
|
|
|
prefixlen <= V4PREFIXMAX) {
|
2012-09-04 12:54:00 +00:00
|
|
|
ipaddr.family = AF_INET;
|
2012-12-19 23:00:00 +00:00
|
|
|
if (res)
|
2012-09-04 12:54:00 +00:00
|
|
|
memcpy(res, &ipaddr, sizeof(ipaddr));
|
|
|
|
return token_len;
|
|
|
|
}
|
|
|
|
if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
|
2018-01-29 10:29:03 +00:00
|
|
|
inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
|
2012-09-04 12:54:00 +00:00
|
|
|
ipaddr.family = AF_INET6;
|
2012-12-19 23:00:00 +00:00
|
|
|
if (res)
|
2012-09-04 12:54:00 +00:00
|
|
|
memcpy(res, &ipaddr, sizeof(ipaddr));
|
|
|
|
return token_len;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
|
|
|
|
unsigned int size)
|
|
|
|
{
|
2012-12-19 23:00:00 +00:00
|
|
|
struct cmdline_token_ipaddr *tk2;
|
|
|
|
|
|
|
|
if (!tk || !dstbuf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tk2 = (struct cmdline_token_ipaddr *)tk;
|
2012-09-04 12:54:00 +00:00
|
|
|
|
|
|
|
switch (tk2->ipaddr_data.flags) {
|
|
|
|
case CMDLINE_IPADDR_V4:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPv4");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
case CMDLINE_IPADDR_V6:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPv6");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
case CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPv4/IPv6");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPv4 network");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V6:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPv6 network");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
case CMDLINE_IPADDR_NETWORK|CMDLINE_IPADDR_V4|CMDLINE_IPADDR_V6:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPv4/IPv6 network");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(dstbuf, size, "IPaddr (bad flags)");
|
2012-09-04 12:54:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|