Skip to content

Commit

Permalink
fixed cidr matching and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bef committed Aug 7, 2021
1 parent 0156cbf commit a997aac
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ int parse_cidr(char *restrict line, char *restrict keyword, void *retval) {

sp_cidr *cidr = pecalloc(sizeof(sp_cidr), 1, 1);

if (0 == get_ip_and_cidr(ZSTR_VAL(value), cidr)) {
if (0 != get_ip_and_cidr(ZSTR_VAL(value), cidr)) {
pefree(cidr, 1);
*(sp_cidr **)retval = NULL;
return -1;
Expand Down
10 changes: 9 additions & 1 deletion src/sp_network_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ int get_ip_and_cidr(char *ip, sp_cidr *cidr) {
return -1;
}

if (sscanf(mask + 1, "%hhu", &(cidr->mask)) != 1) {
int masklen = strlen(mask+1);
int imask = atoi(mask+1);
if (masklen < 1 || masklen > 3 || !isdigit(*(mask+1)) || (masklen >= 2 && !isdigit(*(mask+2))) || (masklen == 3 && !isdigit(*(mask+3))) || imask < 0 || imask > 128) {
sp_log_err("config", "'%s' isn't a valid network mask.", mask + 1);
return -1;
}
cidr->mask = (uint8_t)imask;

ip[mask - ip] = '\0'; // NULL the '/' char

Expand All @@ -113,5 +116,10 @@ int get_ip_and_cidr(char *ip, sp_cidr *cidr) {
}

ip[mask - ip] = '/';
if (cidr->ip_version < 0) {
sp_log_err("cidr_match", "Weird ip (%s) family", ip);
return -1;
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ Broken configuration, cidr for ipv6 is too big, that will `mod` to 25.
(13337%128 = 25)
--SKIPIF--
<?php if (!extension_loaded("snuffleupagus")) print "skip"; ?>
<?php if (PHP_VERSION_ID >= 80000) print "skip"; ?>
--INI--
sp.configuration_file={PWD}/config/broken_conf_invalid_cidr6_too_big.ini
--FILE--
--EXPECT--

Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0
Could not startup.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Broken configuration
sp.configuration_file={PWD}/config/broken_conf_invalid_cidr.ini
--FILE--
--EXPECT--

Fatal error: [snuffleupagus][0.0.0.0][config][log] '42' isn't a valid ipv4 mask. in Unknown on line 0

Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0
Could not startup.
Could not startup.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ Broken configuration, cidr for ipv6 is too big, that will `mod` to 25.
sp.configuration_file={PWD}/config/broken_conf_invalid_cidr6_too_big.ini
--FILE--
--EXPECT--
Fatal error: [snuffleupagus][0.0.0.0][config][log] '13337' isn't a valid network mask. in Unknown on line 0

Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0
Could not startup.

0 comments on commit a997aac

Please sign in to comment.