Skip to content
Open
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
35 changes: 20 additions & 15 deletions libcfnet/addr_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ int FuzzySetMatch(const char *s1, const char *s2)

if (isCIDR)
{
int blocks;
struct sockaddr_in6 addr1 = {0};
struct sockaddr_in6 addr2 = {0};
unsigned long mask;
Expand All @@ -236,26 +235,38 @@ int FuzzySetMatch(const char *s1, const char *s2)
Log(LOG_LEVEL_ERR, "Invalid IPv6 CIDR: %s", s1);
return -1;
}
blocks = mask / 8;
int blocks = mask / 8;
int remaining_bits = mask % 8;

if (mask % 8 != 0)
addr1.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, address, &addr1.sin6_addr) != 1)
{
Log(LOG_LEVEL_ERR, "Cannot handle ipv6 masks which are not 8 bit multiples (fix me)");
return -1;
}

addr1.sin6_family = AF_INET6;
inet_pton(AF_INET6, address, &addr1.sin6_addr);
addr2.sin6_family = AF_INET6;
inet_pton(AF_INET6, s2, &addr2.sin6_addr);
if (inet_pton(AF_INET6, s2, &addr2.sin6_addr) != 1)
{
return -1;
}

for (i = 0; i < blocks; i++) /* blocks < 16 */
for (i = 0; i < blocks; i++)
{
if (addr1.sin6_addr.s6_addr[i] != addr2.sin6_addr.s6_addr[i])
{
return -1;
}
}

if (remaining_bits > 0)
{
uint8_t bitmask = (uint8_t)(0xFF << (8 - remaining_bits));

if ((addr1.sin6_addr.s6_addr[blocks] & bitmask) !=
(addr2.sin6_addr.s6_addr[blocks] & bitmask))
{
return -1;
}
}
return 0;
}
else
Expand Down Expand Up @@ -530,12 +541,6 @@ bool FuzzyMatchParse(const char *s)
mask = 0;
sscanf(s, "%40[^/]/%d", address, &mask);

if (mask % 8 != 0)
{
Log(LOG_LEVEL_ERR, "Cannot handle ipv6 masks which are not 8 bit multiples (fix me)");
return false;
}

if (mask > 15)
{
Log(LOG_LEVEL_ERR, "IPv6 CIDR mask is too large");
Expand Down
27 changes: 27 additions & 0 deletions tests/acceptance/02_classes/02_functions/isipinsubnet_mask.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#######################################################
#
# Test isipinsubnet()
#
#######################################################
body common control
{
bundlesequence => { "check" };
version => "1.0";
}

#######################################################
bundle agent check
{
classes:
"ok"
expression => isipinsubnet(
"2001:700:100::/41", "2001:0700:0100:0000:0000:0000:0000:0000"
);

reports:
ok::
"$(this.promise_filename) Pass";

!ok::
"$(this.promise_filename) FAIL";
}
Loading