Skip to content

Commit

Permalink
Merge pull request #2513 from jimklimov/issue-2244-auto
Browse files Browse the repository at this point in the history
Teach `nut-scanner` to discover locally connected subnets: implement `-m auto` mode
  • Loading branch information
jimklimov authored Jul 8, 2024
2 parents e24e580 + 822dae0 commit 4a71a9b
Show file tree
Hide file tree
Showing 12 changed files with 564 additions and 178 deletions.
7 changes: 7 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ https://github.com/networkupstools/nut/milestone/11
+
NOTE: Currently both the long and short names for `libupsclient` should be
installed.
* newly added support to scan several IP addresses (single or ranges)
with the same call, by repeating command-line options; also `-m auto{,4,6}`
can be specified (once) to select IP (all, IPv4, IPv6) address ranges of
configured local network interfaces (currently not implemented for WIN32).
[issue #2244, PR #2509, PR #2513]
* bumped version of `libnutscan` to 2.5.2, it now includes a few more
methods and symbols from `libcommon`. [issue #2244, PR #2509]
- common code:
* introduced a `NUT_DEBUG_SYSLOG` environment variable to tweak activation
Expand Down
44 changes: 35 additions & 9 deletions docs/man/nut-scanner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,38 +78,59 @@ an 'end IP'. See specific SNMP OPTIONS for community and security settings.

*-M* | *--xml_scan*::
Scan XML/HTTP devices. Can broadcast a network message on the current network
interfaces to retrieve XML/HTTP capable devices. No IP required in this mode.
interface(s) to retrieve XML/HTTP capable devices. No IP required in this mode.
If IP address ranges are specified, they would be scanned instead of a broadcast.

*-O* | *--oldnut_scan*::
Scan NUT devices (i.e. upsd daemon) on IP ranging from 'start IP' to 'end IP'.

*-n* | *--nut_simulation_scan*::
Scan NUT simulated devices (.dev files in $CONFPATH).
Scan NUT simulated devices (`.dev` files in `$NUT_CONFPATH`).

*-A* | *--avahi_scan*::
Scan NUT servers using Avahi request on the current network interfaces.
No IP required.
Scan NUT servers using Avahi request on the current network interface(s).
No IP address options are required or used.

*-I* | *--ipmi_scan*::
Scan NUT compatible power supplies available via IPMI on the current host,
or over the network.
or over the network if IP address ranges are specified.

*-E* | *--eaton_serial* 'serial ports'::
Scan Eaton devices (XCP and SHUT) available via serial bus on the current host.
This option must be requested explicitly, even for a complete scan.
'serial ports' can be expressed in various forms:

+
- 'auto' to scan all serial ports.
- a single character indicating a port number ('0' (zero) for /dev/ttyS0 and
/dev/ttyUSB0 on Linux, '1' for COM1 on Windows, 'a' for /dev/ttya on Solaris...)
/dev/ttyUSB0 on Linux, '1' for COM1 on Windows, 'a' for /dev/ttya on Solaris...)
- a range of N characters, hyphen separated, describing the range of
ports using 'X-Y', where X and Y are characters referring to the port number.
ports using 'X-Y', where X and Y are characters referring to the port number.
- a single port name.
- a list of ports name, coma separated, like '/dev/ttyS1,/dev/ttyS4'.

NETWORK OPTIONS
---------------

NOTE: The networked buses (such as SNMP, NetXML, IPMI and "Old NUT") allow to
specify several IP (IPv4 or IPv6) address ranges, down to individual single
IP addresses. Normally a new range is specified by a set of one `-s` and one
`-e` options following each other (in any order). Lone or consecutive `-s` or
`-e` options present on the command line would translate to single-IP queries.
Also a `-m` option squashed between two `-s` and `-e` options would be a new
range, turning those two into single-IP queries. This feature does not by
itself recombine "neighboring" addresses into one range, nor even check for
duplicate or overlapping specifications.
+
Also note that some buses require IP address(es) to scan, and others have a
different behavior when exactly no addresses are specified (it is not currently
possible to mix the two behaviors in one invocation of the `nut-scanner` tool).
+
Finally note that currently even if multi-threaded support is available, each
range specification is a separate fan-out of queries constrained by the timeout.
Requests to scan many single IP addresses will take a while to complete, much
longer than if they were a single range. This will be hopefully fixed in later
releases.

*-t* | *--timeout* 'timeout'::
Set the network timeout in seconds. Default timeout is 5 seconds.

Expand All @@ -124,7 +145,12 @@ If this parameter is omitted, only the 'start IP' is scanned. If 'end IP' is
less than 'start IP', both parameters are internally permuted.

*-m* | *--mask_cidr* 'IP address/mask'::
Set a range of IP using CIDR notation.
Set a range of IP addresses by using CIDR notation.
+
A special form `-m auto` allows `nut-scanner` to detect local IP address(es)
and scan corresponding subnet(s) on supported platforms, and `-m auto4` or
`-m auto6` limits the selected addresses to IPv4 and IPv6 respectively. Only
the first "auto*" request would be honoured, others ignored with a warning.

NUT DEVICE OPTION
-----------------
Expand Down
3 changes: 2 additions & 1 deletion docs/nut.dict
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
personal_ws-1.1 en 3177 utf-8
personal_ws-1.1 en 3178 utf-8
AAC
AAS
ABI
Expand Down Expand Up @@ -754,6 +754,7 @@ NetInvent
NetPro
NetServer
NetUps
NetXML
Netman
NetworkUPSTools
Neus
Expand Down
41 changes: 4 additions & 37 deletions drivers/libshut.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,10 @@ static int libshut_open(
usb_ctrl_charbuf rdbuf, usb_ctrl_charbufsize rdlen))
{
int ret, res;
/* Below we cast this buffer as sometimes containing entried of type
* "struct device_descriptor_s" or "struct my_hid_descriptor".
* Currently both of these are sized "2", and I don't see a way
* to require a "max()" of such sizes to align for generally.
*/
usb_ctrl_char buf[20] __attribute__((aligned(4)));
char string[MAX_STRING_SIZE];
struct my_hid_descriptor *desc;
struct device_descriptor_s *dev_descriptor;
struct my_hid_descriptor desc_buf, *desc = &desc_buf;
struct device_descriptor_s dev_descriptor_buf, *dev_descriptor = &dev_descriptor_buf;

/* report descriptor */
usb_ctrl_char rdbuf[MAX_REPORT_SIZE];
Expand Down Expand Up @@ -470,21 +465,7 @@ static int libshut_open(
}

/* Get DEVICE descriptor */
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wcast-align"
#endif
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wcast-align"
#endif
dev_descriptor = (struct device_descriptor_s *)buf;
#ifdef __clang__
# pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
# pragma GCC diagnostic pop
#endif
memcpy(dev_descriptor, buf, sizeof(struct device_descriptor_s));
res = shut_get_descriptor(*arg_upsfd, USB_DT_DEVICE, 0, buf, USB_DT_DEVICE_SIZE);
/* res = shut_control_msg(devp, USB_ENDPOINT_IN+1, USB_REQ_GET_DESCRIPTOR,
(USB_DT_DEVICE << 8) + 0, 0, buf, 0x9, SHUT_TIMEOUT); */
Expand Down Expand Up @@ -587,21 +568,7 @@ static int libshut_open(
}

/* Get HID descriptor */
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wcast-align"
#endif
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wcast-align"
#endif
desc = (struct my_hid_descriptor *)buf;
#ifdef __clang__
# pragma clang diagnostic pop
#endif
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_CAST_ALIGN)
# pragma GCC diagnostic pop
#endif
memcpy(desc, buf, sizeof(struct my_hid_descriptor));
res = shut_get_descriptor(*arg_upsfd, USB_DT_HID, hid_desc_index, buf, 0x9);
/* res = shut_control_msg(devp, USB_ENDPOINT_IN+1, USB_REQ_GET_DESCRIPTOR,
(USB_DT_HID << 8) + 0, 0, buf, 0x9, SHUT_TIMEOUT); */
Expand Down
4 changes: 2 additions & 2 deletions tools/nut-scanner/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ endif HAVE_WINDOWS
# object .so names would differ)
#
# libnutscan version information
libnutscan_la_LDFLAGS += -version-info 2:5:1
libnutscan_la_LDFLAGS += -version-info 2:5:2

# libnutscan exported symbols regex
# WARNING: Since the library includes parts of libcommon (as much as needed
Expand All @@ -94,7 +94,7 @@ libnutscan_la_LDFLAGS += -version-info 2:5:1
# copies of "nut_debug_level" making fun of our debug-logging attempts.
# One solution to tackle if needed for those cases would be to make some
# dynamic/shared libnutcommon (etc.)
libnutscan_la_LDFLAGS += -export-symbols-regex '^(nutscan_|nut_debug_level|s_upsdebugx|max_threads|curr_threads|nut_report_config_flags|upsdebugx_report_search_paths|nut_prepare_search_paths)'
libnutscan_la_LDFLAGS += -export-symbols-regex '^(nutscan_|nut_debug_level|s_upsdebugx|fatalx|xcalloc|snprintfcat|max_threads|curr_threads|nut_report_config_flags|upsdebugx_report_search_paths|nut_prepare_search_paths)'
libnutscan_la_CFLAGS = -I$(top_srcdir)/clients -I$(top_srcdir)/include \
$(LIBLTDL_CFLAGS) -I$(top_srcdir)/drivers

Expand Down
Loading

0 comments on commit 4a71a9b

Please sign in to comment.