[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1516299227-26724-3-git-send-email-serhe.popovych@gmail.com>
Date: Thu, 18 Jan 2018 20:13:43 +0200
From: Serhey Popovych <serhe.popovych@...il.com>
To: netdev@...r.kernel.org
Subject: [PATCH iproute2 2/6] utils: Always specify family and ->bytelen in get_prefix_1()
Handle default/all/any special case in get_addr_1() to setup
->family and ->bytelen correctly.
Make get_addr_1() return ->bitlen == -2 instead of -1 to
distinguish default/all/any special case from the rest:
it is safe because all callers check ->bitlen < 0, not
explicit value -1.
Reduce intendation by one level and get rid of goto/label
to make code more readable.
Signed-off-by: Serhey Popovych <serhe.popovych@...il.com>
---
lib/utils.c | 64 +++++++++++++++++++++++++++++------------------------------
1 file changed, 32 insertions(+), 32 deletions(-)
diff --git a/lib/utils.c b/lib/utils.c
index a023e74..48c4bcb 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -545,7 +545,7 @@ int get_addr_1(inet_prefix *addr, const char *name, int family)
return -1;
addr->family = (family != AF_UNSPEC) ? family : AF_INET;
addr->bytelen = af_byte_len(addr->family);
- addr->bitlen = -1;
+ addr->bitlen = -2;
return 0;
}
@@ -644,46 +644,46 @@ int af_byte_len(int af)
int get_prefix_1(inet_prefix *dst, char *arg, int family)
{
- int err;
- unsigned int plen;
char *slash;
-
- memset(dst, 0, sizeof(*dst));
-
- if (strcmp(arg, "default") == 0 ||
- strcmp(arg, "any") == 0 ||
- strcmp(arg, "all") == 0) {
- if ((family == AF_DECnet) || (family == AF_MPLS))
- return -1;
- dst->family = family;
- dst->bytelen = 0;
- dst->bitlen = 0;
- dst->flags |= PREFIXLEN_SPECIFIED;
- return 0;
- }
+ int err, bitlen, flags;
slash = strchr(arg, '/');
if (slash)
*slash = 0;
err = get_addr_1(dst, arg, family);
- if (err == 0) {
- dst->bitlen = af_bit_len(dst->family);
-
- if (slash) {
- if (get_netmask(&plen, slash+1, 0)
- || plen > dst->bitlen) {
- err = -1;
- goto done;
- }
- dst->flags |= PREFIXLEN_SPECIFIED;
- dst->bitlen = plen;
- }
- }
-done:
+
if (slash)
*slash = '/';
- return err;
+
+ if (err)
+ return err;
+
+ bitlen = af_bit_len(dst->family);
+
+ flags = PREFIXLEN_SPECIFIED;
+ if (slash) {
+ unsigned int plen;
+
+ if (dst->bitlen == -2)
+ return -1;
+ if (get_netmask(&plen, slash + 1, 0))
+ return -1;
+ if (plen > bitlen)
+ return -1;
+
+ bitlen = plen;
+ } else {
+ if (dst->bitlen == -2)
+ bitlen = 0;
+ else
+ flags = 0;
+ }
+
+ dst->flags |= flags;
+ dst->bitlen = bitlen;
+
+ return 0;
}
static const char *family_name_verbose(int family)
--
1.7.10.4
Powered by blists - more mailing lists