utils: add get_be{16, 32, 64}, use them where possible

Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Acked-by: Phil Sutter <phil@nwl.cc>
This commit is contained in:
Sabrina Dubroca 2016-06-03 16:45:46 +02:00 committed by Stephen Hemminger
parent 89ae502056
commit 9f7401fa49
10 changed files with 54 additions and 41 deletions

View File

@ -112,6 +112,9 @@ int get_u16(__u16 *val, const char *arg, int base);
int get_s16(__s16 *val, const char *arg, int base);
int get_u8(__u8 *val, const char *arg, int base);
int get_s8(__s8 *val, const char *arg, int base);
int get_be64(__be64 *val, const char *arg, int base);
int get_be32(__be32 *val, const char *arg, int base);
int get_be16(__be16 *val, const char *arg, int base);
int get_addr64(__u64 *ap, const char *cp);
char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);

View File

@ -55,9 +55,8 @@ static int fou_parse_opt(int argc, char **argv, struct nlmsghdr *n,
if (!matches(*argv, "port")) {
NEXT_ARG();
if (get_u16(&port, *argv, 0) || port == 0)
if (get_be16(&port, *argv, 0) || port == 0)
invarg("invalid port", *argv);
port = htons(port);
port_set = 1;
} else if (!matches(*argv, "ipproto")) {
struct protoent *servptr;

View File

@ -172,16 +172,12 @@ static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
invarg("max addresses", *argv);
} else if (!matches(*argv, "port") ||
!matches(*argv, "srcport")) {
__u16 minport, maxport;
NEXT_ARG();
if (get_u16(&minport, *argv, 0))
if (get_be16(&range.low, *argv, 0))
invarg("min port", *argv);
NEXT_ARG();
if (get_u16(&maxport, *argv, 0))
if (get_be16(&range.high, *argv, 0))
invarg("max port", *argv);
range.low = htons(minport);
range.high = htons(maxport);
} else if (!matches(*argv, "dstport")) {
NEXT_ARG();
if (get_u16(&dstport, *argv, 0))

View File

@ -190,9 +190,9 @@ static int parse_encap_ip(struct rtattr *rta, size_t len, int *argcp, char ***ar
NEXT_ARG();
if (id_ok++)
duparg2("id", *argv);
if (get_u64(&id, *argv, 0))
if (get_be64(&id, *argv, 0))
invarg("\"id\" value is invalid\n", *argv);
rta_addattr64(rta, len, LWTUNNEL_IP_ID, htonll(id));
rta_addattr64(rta, len, LWTUNNEL_IP_ID, id);
} else if (strcmp(*argv, "dst") == 0) {
inet_prefix addr;
@ -267,9 +267,9 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len, int *argcp, char ***a
NEXT_ARG();
if (id_ok++)
duparg2("id", *argv);
if (get_u64(&id, *argv, 0))
if (get_be64(&id, *argv, 0))
invarg("\"id\" value is invalid\n", *argv);
rta_addattr64(rta, len, LWTUNNEL_IP6_ID, htonll(id));
rta_addattr64(rta, len, LWTUNNEL_IP6_ID, id);
} else if (strcmp(*argv, "dst") == 0) {
inet_prefix addr;

View File

@ -1109,15 +1109,10 @@ int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
} else if (strcmp(*argv, "spi") == 0) {
__u32 spi;
NEXT_ARG();
if (get_u32(&spi, *argv, 0))
if (get_be32(&id->spi, *argv, 0))
invarg("SPI value is invalid", *argv);
spi = htonl(spi);
id->spi = spi;
filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
} else {
@ -1252,9 +1247,8 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
NEXT_ARG();
if (get_u16(&sel->sport, *argv, 0))
if (get_be16(&sel->sport, *argv, 0))
invarg("value after \"sport\" is invalid", *argv);
sel->sport = htons(sel->sport);
if (sel->sport)
sel->sport_mask = ~((__u16)0);
@ -1265,9 +1259,8 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
NEXT_ARG();
if (get_u16(&sel->dport, *argv, 0))
if (get_be16(&sel->dport, *argv, 0))
invarg("value after \"dport\" is invalid", *argv);
sel->dport = htons(sel->dport);
if (sel->dport)
sel->dport_mask = ~((__u16)0);

View File

@ -175,11 +175,9 @@ static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
int argc = *argcp;
char **argv = *argvp;
if (get_u32(seq, *argv, 0))
if (get_be32(seq, *argv, 0))
invarg("SEQ value is invalid", *argv);
*seq = htonl(*seq);
*argcp = argc;
*argvp = argv;
@ -359,13 +357,11 @@ static int xfrm_state_modify(int cmd, unsigned int flags, int argc, char **argv)
NEXT_ARG();
xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
NEXT_ARG();
if (get_u16(&encap.encap_sport, *argv, 0))
if (get_be16(&encap.encap_sport, *argv, 0))
invarg("SPORT value after \"encap\" is invalid", *argv);
encap.encap_sport = htons(encap.encap_sport);
NEXT_ARG();
if (get_u16(&encap.encap_dport, *argv, 0))
if (get_be16(&encap.encap_dport, *argv, 0))
invarg("DPORT value after \"encap\" is invalid", *argv);
encap.encap_dport = htons(encap.encap_dport);
NEXT_ARG();
get_addr(&oa, *argv, AF_UNSPEC);
memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));

View File

@ -111,8 +111,7 @@ int ll_proto_a2n(unsigned short *id, const char *buf)
return 0;
}
}
if (get_u16(id, buf, 0))
if (get_be16(id, buf, 0))
return -1;
*id = htons(*id);
return 0;
}

View File

@ -353,6 +353,39 @@ int get_s8(__s8 *val, const char *arg, int base)
return 0;
}
int get_be64(__be64 *val, const char *arg, int base)
{
__u64 v;
int ret = get_u64(&v, arg, base);
if (!ret)
*val = htonll(v);
return ret;
}
int get_be32(__be32 *val, const char *arg, int base)
{
__u32 v;
int ret = get_u32(&v, arg, base);
if (!ret)
*val = htonl(v);
return ret;
}
int get_be16(__be16 *val, const char *arg, int base)
{
__u16 v;
int ret = get_u16(&v, arg, base);
if (!ret)
*val = htons(v);
return ret;
}
/* This uses a non-standard parsing (ie not inet_aton, or inet_pton)
* because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8
*/

View File

@ -150,11 +150,11 @@ static int flower_parse_port(char *str, __u8 ip_port,
return -1;
}
ret = get_u16(&port, str, 10);
ret = get_be16(&port, str, 10);
if (ret)
return -1;
addattr16(n, MAX_MSG, type, htons(port));
addattr16(n, MAX_MSG, type, port);
return 0;
}

View File

@ -767,12 +767,9 @@ static int parse_offset(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
}
sel->flags |= TC_U32_VAROFFSET;
} else if (matches(*argv, "mask") == 0) {
__u16 mask;
NEXT_ARG();
if (get_u16(&mask, *argv, 16))
if (get_be16(&sel->offmask, *argv, 16))
return -1;
sel->offmask = htons(mask);
sel->flags |= TC_U32_VAROFFSET;
} else if (matches(*argv, "shift") == 0) {
int shift;
@ -802,12 +799,9 @@ static int parse_hashkey(int *argc_p, char ***argv_p, struct tc_u32_sel *sel)
while (argc > 0) {
if (matches(*argv, "mask") == 0) {
__u32 mask;
NEXT_ARG();
if (get_u32(&mask, *argv, 16))
if (get_be32(&sel->hmask, *argv, 16))
return -1;
sel->hmask = htonl(mask);
} else if (matches(*argv, "at") == 0) {
int num;