Skip to content

Commit

Permalink
reorganize add_cidr()/add_range()
Browse files Browse the repository at this point in the history
This creates a code structure suitable to add IPv6 versions
of the functions used to generate addresses.
  • Loading branch information
auerswal committed Jan 15, 2025
1 parent e0d2b69 commit 88510d2
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ void ev_enqueue(struct event_queue *queue, struct event *event);
struct event *ev_dequeue(struct event_queue *queue);
void ev_remove(struct event_queue *queue, struct event *event);
void add_cidr(char *);
void add_cidr_ipv4(unsigned long, unsigned long);
void add_range(char *, char *);
void add_addr_range_ipv4(unsigned long, unsigned long);
void print_warning(char *fmt, ...);
Expand Down Expand Up @@ -1289,12 +1290,10 @@ void add_cidr(char *addr)
char *addr_end;
char *mask_str;
unsigned long mask;
unsigned long bitmask;
int ret;
struct addrinfo addr_hints;
struct addrinfo *addr_res;
unsigned long net_addr;
unsigned long net_last;

/* Split address from mask */
addr_end = strchr(addr, '/');
Expand All @@ -1314,16 +1313,25 @@ void add_cidr(char *addr)
fprintf(stderr, "%s, can't parse address %s: %s\n", prog, addr, gai_strerror(ret));
exit(1);
}
if (addr_res->ai_family != AF_INET) {
if (addr_res->ai_family == AF_INET) {
net_addr = ntohl(((struct sockaddr_in*)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);
add_cidr_ipv4(net_addr, mask);
} else {
freeaddrinfo(addr_res);
fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog);
exit(1);
}
net_addr = ntohl(((struct sockaddr_in *)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);
}

void add_cidr_ipv4(unsigned long net_addr, unsigned long mask)
{
unsigned long bitmask;
unsigned long net_last;

/* check mask */
if (mask < 1 || mask > 32) {
fprintf(stderr, "%s: netmask must be between 1 and 32 (is: %s)\n", prog, mask_str);
fprintf(stderr, "%s: netmask must be between 1 and 32 (is: %lu)\n", prog, mask);
exit(1);
}

Expand Down Expand Up @@ -1361,13 +1369,16 @@ void add_range(char *start, char *end)
fprintf(stderr, "%s: can't parse address %s: %s\n", prog, start, gai_strerror(ret));
exit(1);
}
if (addr_res->ai_family != AF_INET) {
/* start and end must be from the same address family */
hints_ai_family = addr_res->ai_family;
if (addr_res->ai_family == AF_INET) {
start_long = ntohl(((struct sockaddr_in*)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);
} else {
freeaddrinfo(addr_res);
fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog);
exit(1);
}
hints_ai_family = addr_res->ai_family;
start_long = ntohl(((struct sockaddr_in *)addr_res->ai_addr)->sin_addr.s_addr);

/* parse end address (IPv4 only) */
memset(&addr_hints, 0, sizeof(struct addrinfo));
Expand All @@ -1378,16 +1389,15 @@ void add_range(char *start, char *end)
fprintf(stderr, "%s: can't parse address %s: %s\n", prog, end, gai_strerror(ret));
exit(1);
}
if (addr_res->ai_family != AF_INET) {
if (addr_res->ai_family == AF_INET) {
end_long = ntohl(((struct sockaddr_in*)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);
add_addr_range_ipv4(start_long, end_long);
} else {
freeaddrinfo(addr_res);
fprintf(stderr, "%s: -g works only with IPv4 addresses\n", prog);
exit(1);
}
end_long = ntohl(((struct sockaddr_in *)addr_res->ai_addr)->sin_addr.s_addr);
freeaddrinfo(addr_res);

/* 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)
Expand Down

0 comments on commit 88510d2

Please sign in to comment.