From: Serhey Popovych <serhe.popovych@gmail.com>
To: netdev@vger.kernel.org
Subject: [PATCH iproute2 2/6] utils: Always specify family and ->bytelen in get_prefix_1()
Date: Thu, 18 Jan 2018 20:13:43 +0200 [thread overview]
Message-ID: <1516299227-26724-3-git-send-email-serhe.popovych@gmail.com> (raw)
In-Reply-To: <1516299227-26724-1-git-send-email-serhe.popovych@gmail.com>
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@gmail.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
next prev parent reply other threads:[~2018-01-18 18:14 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-18 18:13 [PATCH iproute2 0/6] utils: Get rid of inet_get_addr() Serhey Popovych
2018-01-18 18:13 ` [PATCH iproute2 1/6] utils: Always specify family for address in get_addr_1() Serhey Popovych
2018-01-18 18:13 ` Serhey Popovych [this message]
2018-01-18 18:13 ` [PATCH iproute2 3/6] utils: Fast inet address classification after get_addr() Serhey Popovych
2018-01-18 18:13 ` [PATCH iproute2 4/6] iplink_geneve: Get rid of inet_get_addr() Serhey Popovych
2018-01-18 18:13 ` [PATCH iproute2 5/6] iplink_vxlan: " Serhey Popovych
2018-01-18 18:13 ` [PATCH iproute2 6/6] ip: " Serhey Popovych
2018-01-18 18:35 ` [PATCH iproute2 0/6] utils: " Stephen Hemminger
2018-01-21 18:13 ` David Ahern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1516299227-26724-3-git-send-email-serhe.popovych@gmail.com \
--to=serhe.popovych@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox