Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teach nut-scanner -m auto*/ADDRLEN modes #2517

Merged
merged 21 commits into from
Jul 9, 2024

Conversation

jimklimov
Copy link
Member

@jimklimov jimklimov commented Jul 8, 2024

Follow-up for PRs #2509 and #2513, of issue #2244.

This PR adds a way to constrain the scan amounts in discovered subnets by specifying the bit-width of the host address part (e.g. in a typical IPv4 /24 subnet, that part of the option value would be 32-24 = 8 => -m auto4/8).

Also it now reports detailed reasons why this or that discovered subnet was ignored (too large, loopback, not up and running...)

Detailed "screenshot" from a test run:

:; make -ks -j && LD_LIBRARY_PATH=`pwd`/clients/.libs ./tools/nut-scanner/nut-scanner -DDDDDD -O -m auto4/4
...
   0.080064     [D5] Discovering getifaddrs(): Interface: lo    Address: 127.0.0.1      Mask: 255.0.0.0 (subnet: 8, hosts: 24)  Flags: 00010049 IFF_LOOPBACK IFF_UP IFF_RUNNING
   0.080106     [D6] Subnet ignored: address range too large: 4 bits allowed vs. 24 bits per netmask
   0.080138     [D5] Discovering getifaddrs(): Interface: eth0  Address: 172.28.79.185  Mask: 255.255.240.0 (subnet: 20, hosts: 12)     Flags: 00011043 IFF_UP IFF_RUNNING IFF_BROADCAST(is assigned)
   0.080145     [D6] Subnet ignored: address range too large: 4 bits allowed vs. 12 bits per netmask
   0.080148     [D5] Discovering getifaddrs(): Interface: eth0  Address: 172.22.48.99   Mask: 255.255.240.0 (subnet: 20, hosts: 12)     Flags: 00011043 IFF_UP IFF_RUNNING IFF_BROADCAST(is assigned)
   0.080156     [D6] Subnet ignored: address range too large: 4 bits allowed vs. 12 bits per netmask
   0.080163     [D5] Discovering getifaddrs(): Interface: lo    Address: ::1    Mask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff (subnet: 128, hosts: 0)   Flags: 00010049 IFF_LOOPBACK IFF_UP IFF_RUNNING
   0.080188     [D6] Subnet ignored: loopback
   0.080218     [D5] Discovering getifaddrs(): Interface: eth0  Address: fe80::215:5dff:fe8b:d950%eth0  Mask: ffff:ffff:ffff:ffff:: (subnet: 64, hosts: 64)     Flags: 00011043 IFF_UP IFF_RUNNING IFF_BROADCAST(is assigned)
   0.080250     [D6] Subnet ignored: address range too large: 4 bits allowed vs. 64 bits per netmask
...

And with -m auto4/16 it did accept some of the address ranges:

   0.065008     [D5] Discovering getifaddrs(): Interface: lo    Address: 127.0.0.1      Mask: 255.0.0.0 (subnet: 8, hosts: 24)  Flags: 00010049 IFF_LOOPBACK IFF_UP IFF_RUNNING
   0.065029     [D6] Subnet ignored: address range too large: 16 bits allowed vs. 24 bits per netmask
   0.065067     [D5] Discovering getifaddrs(): Interface: eth0  Address: 172.28.79.185  Mask: 255.255.240.0 (subnet: 20, hosts: 12)     Flags: 00011043 IFF_UP IFF_RUNNING IFF_BROADCAST(is assigned)
   0.065088     [D5] Processing CIDR net/mask: 172.28.79.185/20
   0.065092     [D5] nutscan_cidr_to_ip: parsed cidr=172.28.79.185/20 into first_ip=172.28.79.185 and mask=20
   0.065094     [D5] nutscan_cidr_to_ip: parsed mask into numeric value 20
   0.065097     [D5] Extracted IP address range from CIDR net/mask: 172.28.64.0 => 172.28.79.255
   0.065099     [D1] Recorded IP address range #1: [172.28.64.0 .. 172.28.79.255]
   0.065122     [D5] Discovering getifaddrs(): Interface: eth0  Address: 172.22.48.99   Mask: 255.255.240.0 (subnet: 20, hosts: 12)     Flags: 00011043 IFF_UP IFF_RUNNING IFF_BROADCAST(is assigned)
   0.065143     [D5] Processing CIDR net/mask: 172.22.48.99/20
   0.065147     [D5] nutscan_cidr_to_ip: parsed cidr=172.22.48.99/20 into first_ip=172.22.48.99 and mask=20
   0.065165     [D5] nutscan_cidr_to_ip: parsed mask into numeric value 20
   0.065170     [D5] Extracted IP address range from CIDR net/mask: 172.22.48.0 => 172.22.63.255
   0.065191     [D1] Recorded IP address range #2: [172.22.48.0 .. 172.22.63.255]
   0.065199     [D5] Discovering getifaddrs(): Interface: lo    Address: ::1    Mask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff (subnet: 128, hosts: 0)   Flags: 00010049 IFF_LOOPBACK IFF_UP IFF_RUNNING
   0.065217     [D6] Subnet ignored: loopback
   0.066865     [D5] Discovering getifaddrs(): Interface: eth0  Address: fe80::215:5dff:fe8b:d950%eth0  Mask: ffff:ffff:ffff:ffff:: (subnet: 64, hosts: 64)     Flags: 00011043 IFF_UP IFF_RUNNING IFF_BROADCAST(is assigned)
   0.066893     [D6] Subnet ignored: address range too large: 16 bits allowed vs. 64 bits per netmask

I've initially considered adding (also?) a mode for layman users to request the amount of scans they would tolerate, e.g. -m auto4#200 that would translate into "200 < 256 => bit length 8 or less is okay for host address part", but per https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2 it is not too trivial - and for IPv6 support to be on par across the board, some fiddling with uint128_t (not ubiquitous) would be needed.

@jimklimov jimklimov added enhancement nut-scanner impacts-release-2.8.2 Issues reported against NUT release 2.8.2 (maybe vanilla or with minor packaging tweaks) labels Jul 8, 2024
@jimklimov jimklimov added this to the 2.8.3 milestone Jul 8, 2024
@AppVeyorBot

This comment was marked as resolved.

@jimklimov
Copy link
Member Author

jimklimov commented Jul 8, 2024

Testing erroneous-input checking:

  • -m autox is let slide:
   0.065437     Got a '-m auto*' CLI option with unsupported keyword pattern; assuming a CIDR, likely to fail: autox
   0.065468     [D5] Processing CIDR net/mask: autox
   0.065472     WARNING: nutscan_cidr_to_ip failed to parse mask from cidr=autox (first_ip=autox)
   0.065474     [D5] Extracted IP address range from CIDR net/mask: (null) => (null)
   0.065476     [D5] add_ip_range: skip, no addresses were provided
  • -m auto/x is fatal:
...
   0.077328     Invalid auto-net limit value, should be an integer [0..128]: auto/x

$ echo $?
1
  • -m auto/146 (bit length too long) also:
   0.064268     Invalid auto-net limit value, should be an integer [0..128]: auto/146

UPDATE: Using strtol() allows for finer-grained checks of invalid input instead of atol() just returning 0, e.g.

   0.072813     [D6] errno=0 s=''(0x7ffc11358000) input='129'(0x7ffc11357ffd) output=129
   0.072835     Invalid auto-net limit value, should be an integer [0..128]: auto/129

   0.087820     [D6] errno=0 s='x'(0x7ffe7d038fff) input='4x'(0x7ffe7d038ffe) output=4
   0.087845     Invalid auto-net limit value, should be an integer [0..128]: auto/4x

   0.079053     [D6] errno=0 s=''(0x7ffee9e67000) input='4'(0x7ffee9e66fff) output=4
   0.079151     [D5] Discovering getifaddrs(): Interface: lo    Address: 127.0.0.1      Mask: 255.0.0.0 (subnet: 8, hosts: 24)  Flags: 00010049 IFF_LOOPBACK IFF_UP IFF_RUNNING
   0.079173     [D6] Subnet ignored: address range too large: 4 bits allowed vs. 24 bits per netmask
...

…ict, NEWS.adoc: handle_arg_cidr(): add `-m auto*/ADDRLEN` mode [networkupstools#2244]

Signed-off-by: Jim Klimov <[email protected]>
… "arg_addr" [networkupstools#2244]

Avoid warning about clash with a global variable.
Fallout of refactoring original code into a method.

Also make it `const` as we do not change the original value anyway.

Signed-off-by: Jim Klimov <[email protected]>
… subnet by family before checking for mask length [networkupstools#2244]

Signed-off-by: Jim Klimov <[email protected]>
@AppVeyorBot
Copy link

… before we declare vars and types

Signed-off-by: Jim Klimov <[email protected]>
… for ip_ranges[] list and helper metadata, and methods as part of libnutscan [networkupstools#2244, networkupstools#2511]

Not bumping library version, because it was recently bumped as
part of other PRs about this issue. Technically the scope of the
library has been changed by new exported methods and header lines.

Signed-off-by: Jim Klimov <[email protected]>
@jimklimov
Copy link
Member Author

jimklimov commented Jul 8, 2024

Exposed the code for IP range lists tracking as part of libnutscan so it can be used elsewhere (preparing for #2511 in particular).

@AppVeyorBot
Copy link

@jimklimov jimklimov merged commit bf8cc0d into networkupstools:master Jul 9, 2024
30 checks passed
@jimklimov jimklimov deleted the issue-2244 branch July 9, 2024 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement impacts-release-2.8.2 Issues reported against NUT release 2.8.2 (maybe vanilla or with minor packaging tweaks) NUT protocols nut-scanner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants