Low: tools/findif: fix comment typos

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Low: tools/findif: make ConvertQuadToInt use netmask_bits fnc

This is not only to simplify ConvertQuadToInt and practice DRY
principle, but also to make it a little bit safer.  Because
of vague data types involved (instead of uint32_t at al.),
this at least adds one assurance and that is that the logic
will not get broken on architecture with 16bit int
(provided that long has still 32bit, which is not guaranteed
either).

Newly, failure of inet_pton (especially, malformed IP address
format) is indicated via -1 return value.  Previously, such
case was not handled safely.

The patch also unifies tab usage (ConvertQuadToInt was the only
function using spaces for indentation).

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Low: tools/findif: split ValidateNetmaskBits (conv.+valid.)

This commits moves conversion logic from ValidateNetmaskBits into
a separate function (ConvertNetmaskBitsToInt).  This way, we can
validate resulting number of netmask's bits, no matter if the original
string specified netmask as IP expression (via ConvertQuadToInt) or
directly as number of its bits (via ConvertNetmaskBitsToInt).

In turn, we are reaching the original goal to remove ill-advised
reuse of the storage with a string obtained by getenv call
(see netmaskbits, its initialization and rewrite using snprintf).

Strictly viewed, pointer to string returned by getenv should rather
not be stored at all (see [1]), but at least, this patch makes
the program conform to "[t]he application shall ensure that it does
not modify the string pointed to by the getenv() function" part
of getent POSIX documentation (per [1], C standard ditto).

[1] https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=1703960

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Low: tools/findif: remove superfluous string termination

The string is already terminated at that position
(see "man fgets strlen").

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Low: tools/findif: sanitize SearchUsingRouteCmd a bit

Not knowing how the input SearchUsingRouteCmd generates and subsequently
processing [*], only the most conservative changes are contained in this
patch.

First, there was a futile assigment that would only be valid if
commented out strsep calls were in effect.  Due to this, the assigment
was also only commented out.

Next, return value used to be taken from the first strtok in sequence,
only to be overwritten by another one.

Finally, if strtok unexpectedly returned NULL (due to input format other
than expected), it was not handled gracefully.

[*] "route -n get <addr>" is not supported in RHEL neither in Fedora

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Low: tools/findif: add test-findif.sh

Basically, this validates previous findif changes.

As can be seen, the patches do not introduce regression (at least in the
tested cases), but additionally fix cases where netmask is specified
in the IP-like but broken form.

Before the changes:

[...]
------ 30) DUMMY_IP+1, explicit *invalid* netmask (missing last item in quad)
OCF_RESKEY_ip=198.51.100.2 OCF_RESKEY_cidr_netmask=255.255.255.
Converted dotted-quad netmask to CIDR as: 6
FAIL exit code: 0 vs 6
[FAIL]
------ 30) DUMMY_IP+1, explicit *invalid* netmask (random string containing dot)
OCF_RESKEY_ip=198.51.100.2 OCF_RESKEY_cidr_netmask=foo.bar
Converted dotted-quad netmask to CIDR as: 6
FAIL exit code: 0 vs 6
[FAIL]
--- TOTAL ---
[FAIL] 2
[...]

After the changes:

[...]
------ 30) DUMMY_IP+1, explicit *invalid* netmask (missing last item in
quad)
OCF_RESKEY_ip=198.51.100.2 OCF_RESKEY_cidr_netmask=255.255.255.
Converted dotted-quad netmask to CIDR as: -1
Invalid netmask specification [255.255.255.]
[ OK ]
------ 30) DUMMY_IP+1, explicit *invalid* netmask (random string
containing dot)
OCF_RESKEY_ip=198.51.100.2 OCF_RESKEY_cidr_netmask=foo.bar
Converted dotted-quad netmask to CIDR as: -1
Invalid netmask specification [foo.bar]
[ OK ]
--- TOTAL ---
[ OK ]
[...]

Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Merge pull request #143 from jnpkrn/patches

 Low: tools/findif: various cleanups