Skip to content

Commit

Permalink
nut-scanner: introduce nutscan_stringify_ip_ranges() [networkupstools…
Browse files Browse the repository at this point in the history
…#2244, networkupstools#2511]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jul 9, 2024
1 parent 777b8ae commit fcafaec
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/man/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ SRC_DEV_PAGES = \
nutscan_display_parsable.txt \
nutscan_init_ip_ranges.txt \
nutscan_free_ip_ranges.txt \
nutscan_stringify_ip_ranges.txt \
nutscan_add_ip_range.txt \
nutscan_ip_ranges_iter_init.txt \
nutscan_ip_ranges_iter_inc.txt \
Expand Down Expand Up @@ -323,6 +324,7 @@ MAN3_DEV_PAGES = \
nutscan_display_parsable.3 \
nutscan_init_ip_ranges.3 \
nutscan_free_ip_ranges.3 \
nutscan_stringify_ip_ranges.3 \
nutscan_add_ip_range.3 \
nutscan_ip_ranges_iter_init.3 \
nutscan_ip_ranges_iter_inc.3 \
Expand Down Expand Up @@ -415,6 +417,7 @@ HTML_DEV_MANS = \
nutscan_display_parsable.html \
nutscan_init_ip_ranges.html \
nutscan_free_ip_ranges.html \
nutscan_stringify_ip_ranges.html \
nutscan_add_ip_range.html \
nutscan_ip_ranges_iter_init.html \
nutscan_ip_ranges_iter_inc.html \
Expand Down
1 change: 1 addition & 0 deletions docs/man/nutscan_add_ip_range.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ SEE ALSO
--------

linkman:nutscan_init_ip_ranges[3], linkman:nutscan_free_ip_ranges[3],
linkman:nutscan_stringify_ip_ranges[3],
linkman:nutscan_cidr_to_ip[3],
linkman:nutscan_ip_ranges_iter_init[3],
linkman:nutscan_ip_ranges_iter_inc[3]
1 change: 1 addition & 0 deletions docs/man/nutscan_free_ip_ranges.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SEE ALSO
--------

linkman:nutscan_init_ip_ranges[3], linkman:nutscan_add_ip_range[3],
linkman:nutscan_stringify_ip_ranges[3],
linkman:nutscan_cidr_to_ip[3],
linkman:nutscan_ip_ranges_iter_init[3],
linkman:nutscan_ip_ranges_iter_inc[3]
1 change: 1 addition & 0 deletions docs/man/nutscan_init_ip_ranges.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SEE ALSO
--------

linkman:nutscan_free_ip_ranges[3], linkman:nutscan_add_ip_range[3],
linkman:nutscan_stringify_ip_ranges[3],
linkman:nutscan_cidr_to_ip[3],
linkman:nutscan_ip_ranges_iter_init[3],
linkman:nutscan_ip_ranges_iter_inc[3]
39 changes: 39 additions & 0 deletions docs/man/nutscan_stringify_ip_ranges.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
NUTSCAN_STRINGIFY_IP_RANGES(3)
==============================

NAME
----

nutscan_stringify_ip_ranges - Collect contents of a `nutscan_ip_range_list_t`
structure into a string buffer that can be further printed into logs.

SYNOPSIS
--------

#include <nut-scan.h>

const char * nutscan_stringify_ip_ranges(nutscan_ip_range_list_t *irl);

DESCRIPTION
-----------

The *nutscan_stringify_ip_ranges()* function can walk a `nutscan_ip_range_list_t`
structure to report its contents: count of list items, and a comma-separated
listing with each item as a single token (if `start_ip==end_ip` in that range)
or a range as `start_ip .. end_ip`.

Returns a pointer to internal statically allocated buffer which would be
overwritten by subsequent calls, but does not have to be freed by caller.

NOTES
-----

Technically, the function is currently defined in 'nutscan-ip.h' file.

SEE ALSO
--------

linkman:nutscan_free_ip_ranges[3], linkman:nutscan_add_ip_range[3],
linkman:nutscan_cidr_to_ip[3],
linkman:nutscan_ip_ranges_iter_init[3],
linkman:nutscan_ip_ranges_iter_inc[3]
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 3185 utf-8
personal_ws-1.1 en 3186 utf-8
AAC
AAS
ABI
Expand Down Expand Up @@ -2831,6 +2831,7 @@ strcpy
strdup
strerror
strftime
stringify
strlen
strnlen
strptime
Expand Down
48 changes: 48 additions & 0 deletions tools/nut-scanner/nutscan-ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,54 @@ size_t nutscan_add_ip_range(nutscan_ip_range_list_t *irl, char * start_ip, char
return irl->ip_ranges_count;
}

const char * nutscan_stringify_ip_ranges(nutscan_ip_range_list_t *irl)
{
static char buf[LARGEBUF - 64]; /* Leave some space for upsdebugx() prefixes */
size_t len = 0;

memset(buf, 0, sizeof(buf));
len += snprintf(buf + len, sizeof(buf) - len,
"(%" PRIuSIZE ")[",
(irl ? irl->ip_ranges_count : 0));

if (irl && irl->ip_ranges && irl->ip_ranges_count) {
nutscan_ip_range_t *p;
size_t j;

for (
j = 0, p = irl->ip_ranges;
p && len < sizeof(buf) - 6;
p = p->next, j++
) {
if (j) {
buf[len++] = ',';
buf[len++] = ' ';
}

if (len > sizeof(buf) - 6) {
/* Too little left, but enough for this */
buf[len++] = '.';
buf[len++] = '.';
buf[len++] = '.';
break;
}

if (p->start_ip == p->end_ip || !strcmp(p->start_ip, p->end_ip)) {
len += snprintf(buf + len, sizeof(buf) - len,
"%s", p->start_ip);
} else {
len += snprintf(buf + len, sizeof(buf) - len,
"%s .. %s", p->start_ip, p->end_ip);
}
}
}

if (len < sizeof(buf) - 1)
buf[len++] = ']';

return buf;
}

/* Return the first ip or NULL if error */
char * nutscan_ip_ranges_iter_init(nutscan_ip_range_list_iter_t *irliter, const nutscan_ip_range_list_t *irl)
{
Expand Down
4 changes: 4 additions & 0 deletions tools/nut-scanner/nutscan-ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ nutscan_ip_range_list_t *nutscan_init_ip_ranges(nutscan_ip_range_list_t *irl);
*/
void nutscan_free_ip_ranges(nutscan_ip_range_list_t *irl);

/* Prints contents of irl into a groovy-like string,
* using a static buffer (rewritten by each call) */
const char * nutscan_stringify_ip_ranges(nutscan_ip_range_list_t *irl);

size_t nutscan_add_ip_range(nutscan_ip_range_list_t *irl, char * start_ip, char * end_ip);

/* Iterator over given nutscan_ip_range_list_t structure
Expand Down

0 comments on commit fcafaec

Please sign in to comment.