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

Generator limit #331

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ci/test-06-options-f-h.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/perl -w

use Test::Command tests => 72;
use Test::Command tests => 75;
use Test::More;
use File::Temp;

Expand Down Expand Up @@ -170,6 +170,14 @@
$cmd->stderr_is_eq("fping: netmask must be between 1 and 32 (is: 0)\n");
}

# fping -g (cidr - too many addresses)
{
my $cmd = Test::Command->new(cmd => "fping -g 127.0.0.0/8");
$cmd->exit_is_num(1);
$cmd->stdout_is_eq("");
$cmd->stderr_is_eq("fping: -g parameter generates too many addresses\n");
}

# fping -g (range - no IPv6 generator)
SKIP: {
if($ENV{SKIP_IPV6}) {
Expand Down
4 changes: 4 additions & 0 deletions doc/fping.pod
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ line arguments, and 4 for a system call failure.

=head1 RESTRICTIONS

The number of addresses that can be generated using the C<-g>, C<--generate>
option is limited to 131070 (the number of host addresses in one 15-bit IPv4
prefix).

If fping was configured with C<--enable-safe-limits>, the following values are
not allowed for non-root users:

Expand Down
26 changes: 14 additions & 12 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extern int h_errno;
#define SIZE_ICMP_HDR 8 /* from ip_icmp.h */
#define MAX_PING_DATA (MAX_IP_PACKET - SIZE_IP_HDR - SIZE_ICMP_HDR)

#define MAX_GENERATE 100000 /* maximum number of hosts that -g can generate */
#define MAX_GENERATE 131070 /* maximum number of hosts that -g can generate */

/* sized so as to be like traditional ping */
#define DEFAULT_PING_DATA_SIZE 56
Expand Down Expand Up @@ -394,6 +394,7 @@ struct event *ev_dequeue(struct event_queue *queue);
void ev_remove(struct event_queue *queue, struct event *event);
void add_cidr(char *);
void add_range(char *, char *);
void add_addr_range_ipv4(unsigned long, unsigned long);
void print_warning(char *fmt, ...);
int addr_cmp(struct sockaddr *a, struct sockaddr *b);
void host_add_ping_event(HOST_ENTRY *h, int index, int64_t ev_time);
Expand Down Expand Up @@ -1282,6 +1283,7 @@ void add_cidr(char *addr)
exit(1);
}
net_addr = ntohl(((struct sockaddr_in *)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);

/* check mask */
if (mask < 1 || mask > 32) {
Expand All @@ -1303,15 +1305,7 @@ void add_cidr(char *addr)
}

/* add all hosts in that network (net_addr and net_last inclusive) */
for (; net_addr <= net_last; net_addr++) {
struct in_addr in_addr_tmp;
char buffer[20];
in_addr_tmp.s_addr = htonl(net_addr);
inet_ntop(AF_INET, &in_addr_tmp, buffer, sizeof(buffer));
add_name(buffer);
}

freeaddrinfo(addr_res);
add_addr_range_ipv4(net_addr, net_last);
}

void add_range(char *start, char *end)
Expand Down Expand Up @@ -1355,7 +1349,14 @@ void add_range(char *start, char *end)
end_long = ntohl(((struct sockaddr_in *)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);

if (end_long > start_long + MAX_GENERATE) {
/* add IPv4 addresses from closed interval [start_long,end_long] */
add_addr_range_ipv4(start_long, end_long);
}

void add_addr_range_ipv4(unsigned long start_long, unsigned long end_long)
{
/* check if generator limit is exceeded */
if (end_long >= start_long + MAX_GENERATE) {
fprintf(stderr, "%s: -g parameter generates too many addresses\n", prog);
exit(1);
}
Expand Down Expand Up @@ -3013,7 +3014,8 @@ void usage(int is_error)
fprintf(out, " -B, --backoff=N set exponential backoff factor to N (default: 1.5)\n");
fprintf(out, " -c, --count=N count mode: send N pings to each target and report stats\n");
fprintf(out, " -f, --file=FILE read list of targets from a file ( - means stdin)\n");
fprintf(out, " -g, --generate generate target list (only if no -f specified)\n");
fprintf(out, " -g, --generate generate target list (only if no -f specified),\n");
fprintf(out, " limited to at most %d targets\n", MAX_GENERATE);
fprintf(out, " (give start and end IP in the target list, or a CIDR address)\n");
fprintf(out, " (ex. %s -g 192.168.1.0 192.168.1.255 or %s -g 192.168.1.0/24)\n", prog, prog);
fprintf(out, " -H, --ttl=N set the IP TTL value (Time To Live hops)\n");
Expand Down
Loading