Skip to content

Commit

Permalink
nut-scanner: convert NetXML scan to use nutscan_ip_ranges_iter*() [#2511
Browse files Browse the repository at this point in the history
]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jul 9, 2024
1 parent 006b1de commit 808e3d3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
22 changes: 1 addition & 21 deletions tools/nut-scanner/nut-scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,31 +214,11 @@ static void * run_snmp(void * arg)
static void * run_xml(void * arg)
{
nutscan_xml_t * sec = (nutscan_xml_t *)arg;
nutscan_device_t * dev_ret;
nutscan_ip_range_t *p = ip_ranges_list.ip_ranges;

upsdebugx(2, "Entering %s for %" PRIuSIZE " IP address range(s)",
__func__, ip_ranges_list.ip_ranges_count);

if (!p) {
/* Probe broadcast */
dev[TYPE_XML] = nutscan_scan_xml_http_range(NULL, NULL, timeout, sec);

upsdebugx(2, "Finished %s query", __func__);
return NULL;
}

dev[TYPE_XML] = NULL;
while (p) {
dev_ret = nutscan_scan_xml_http_range(p->start_ip, p->end_ip, timeout, sec);
if (!dev[TYPE_XML]) {
dev[TYPE_XML] = dev_ret;
} else {
dev[TYPE_XML] = nutscan_rewind_device(
nutscan_add_device_to_device(dev_ret, dev[TYPE_XML]));
}
p = p->next;
}
dev[TYPE_XML] = nutscan_scan_ip_range_xml_http(&ip_ranges_list, timeout, sec);

upsdebugx(2, "Finished %s loop", __func__);
return NULL;
Expand Down
61 changes: 47 additions & 14 deletions tools/nut-scanner/scan_xml_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,31 @@ static void * nutscan_scan_xml_http_generic(void * arg)
}

nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char * end_ip, useconds_t usec_timeout, nutscan_xml_t * sec)
{
nutscan_device_t *ndret;

/* Are we scanning locally, or through the network? */
if (start_ip || end_ip) {
nutscan_ip_range_list_t irl;

nutscan_init_ip_ranges(&irl);
nutscan_add_ip_range(&irl, (char *)start_ip, (char *)end_ip);

ndret = nutscan_scan_ip_range_xml_http(&irl, usec_timeout, sec);

/* Avoid nuking caller's strings here */
irl.ip_ranges->start_ip = NULL;
irl.ip_ranges->end_ip = NULL;
nutscan_free_ip_ranges(&irl);
} else {
/* Probe local device */
ndret = nutscan_scan_ip_range_xml_http(NULL, usec_timeout, sec);
}

return ndret;
}

nutscan_device_t * nutscan_scan_ip_range_xml_http(nutscan_ip_range_list_t * irl, useconds_t usec_timeout, nutscan_xml_t * sec)
{
bool_t pass = TRUE; /* Track that we may spawn a scanning thread */
nutscan_xml_t * tmp_sec = NULL;
Expand All @@ -424,19 +449,18 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
return NULL;
}

if (start_ip == NULL && end_ip != NULL) {
start_ip = end_ip;
}

if (start_ip == NULL) {
if (irl == NULL || irl->ip_ranges == NULL) {
upsdebugx(1, "%s: Scanning XML/HTTP bus using broadcast.", __func__);
} else {
if ((start_ip == end_ip) || (end_ip == NULL) || (0 == strncmp(start_ip, end_ip, 128))) {
if (irl->ip_ranges_count == 1
&& (irl->ip_ranges->start_ip == irl->ip_ranges->end_ip
|| !strcmp(irl->ip_ranges->start_ip, irl->ip_ranges->end_ip)
)) {
upsdebugx(1, "%s: Scanning XML/HTTP bus for single IP address: %s",
__func__, start_ip);
__func__, irl->ip_ranges->start_ip);
} else {
/* Iterate the range of IPs to scan */
nutscan_ip_iter_t ip;
nutscan_ip_range_list_iter_t ip;
char * ip_str = NULL;
#ifdef HAVE_PTHREAD
# ifdef HAVE_SEMAPHORE
Expand All @@ -452,8 +476,8 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
# endif
#endif

upsdebugx(1, "%s: Scanning XML/HTTP bus for IP address range: %s .. %s",
__func__, start_ip, end_ip);
upsdebugx(1, "%s: Scanning XML/HTTP bus for IP address range(s): %s",
__func__, nutscan_stringify_ip_ranges(irl));

#ifdef HAVE_PTHREAD
pthread_mutex_init(&dev_mutex, NULL);
Expand Down Expand Up @@ -489,7 +513,7 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char

#endif /* HAVE_PTHREAD */

ip_str = nutscan_ip_iter_init(&ip, start_ip, end_ip);
ip_str = nutscan_ip_ranges_iter_init(&ip, irl);

while (ip_str != NULL) {
#ifdef HAVE_PTHREAD
Expand Down Expand Up @@ -633,7 +657,7 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
#endif /* if HAVE_PTHREAD */

/* free(ip_str); */ /* One of these free()s seems to cause a double-free instead */
ip_str = nutscan_ip_iter_inc(&ip);
ip_str = nutscan_ip_ranges_iter_inc(&ip);
/* free(tmp_sec); */
} else { /* if not pass -- all slots busy */
#ifdef HAVE_PTHREAD
Expand Down Expand Up @@ -736,10 +760,10 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
}

memcpy(tmp_sec, sec, sizeof(nutscan_xml_t));
if (start_ip == NULL) {
if (irl == NULL || irl->ip_ranges == NULL || irl->ip_ranges->start_ip == NULL) {
tmp_sec->peername = NULL;
} else {
tmp_sec->peername = strdup(start_ip);
tmp_sec->peername = strdup(irl->ip_ranges->start_ip);
}

if (tmp_sec->usec_timeout <= 0) {
Expand All @@ -765,4 +789,13 @@ nutscan_device_t * nutscan_scan_xml_http_range(const char * start_ip, const char
return NULL;
}

/* stub function */
nutscan_device_t * nutscan_scan_ip_range_xml_http(nutscan_ip_range_list_t * irl, useconds_t usec_timeout, nutscan_xml_t * sec)
{
NUT_UNUSED_VARIABLE(irl);
NUT_UNUSED_VARIABLE(usec_timeout);
NUT_UNUSED_VARIABLE(sec);
return NULL;
}

#endif /* WITH_NEON */

0 comments on commit 808e3d3

Please sign in to comment.