diff options
| author | Ben Fuhrmannek | 2021-08-07 18:45:33 +0200 |
|---|---|---|
| committer | Ben Fuhrmannek | 2021-08-07 18:45:33 +0200 |
| commit | a997aac81cdda77028f74211cad2d07489854994 (patch) | |
| tree | acfe9f7d5088adb9dcc5d3219662d5f6f597bf1c /src | |
| parent | 0156cbf1d165a9df8e952cd09e23d2ecc29d3a6e (diff) | |
fixed cidr matching and test cases
Diffstat (limited to 'src')
5 files changed, 19 insertions, 4 deletions
diff --git a/src/sp_config.c b/src/sp_config.c index 667867b..0144b1a 100644 --- a/src/sp_config.c +++ b/src/sp_config.c | |||
| @@ -163,7 +163,7 @@ int parse_cidr(char *restrict line, char *restrict keyword, void *retval) { | |||
| 163 | 163 | ||
| 164 | sp_cidr *cidr = pecalloc(sizeof(sp_cidr), 1, 1); | 164 | sp_cidr *cidr = pecalloc(sizeof(sp_cidr), 1, 1); |
| 165 | 165 | ||
| 166 | if (0 == get_ip_and_cidr(ZSTR_VAL(value), cidr)) { | 166 | if (0 != get_ip_and_cidr(ZSTR_VAL(value), cidr)) { |
| 167 | pefree(cidr, 1); | 167 | pefree(cidr, 1); |
| 168 | *(sp_cidr **)retval = NULL; | 168 | *(sp_cidr **)retval = NULL; |
| 169 | return -1; | 169 | return -1; |
diff --git a/src/sp_network_utils.c b/src/sp_network_utils.c index 31e8426..5b4fd95 100644 --- a/src/sp_network_utils.c +++ b/src/sp_network_utils.c | |||
| @@ -91,10 +91,13 @@ int get_ip_and_cidr(char *ip, sp_cidr *cidr) { | |||
| 91 | return -1; | 91 | return -1; |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | if (sscanf(mask + 1, "%hhu", &(cidr->mask)) != 1) { | 94 | int masklen = strlen(mask+1); |
| 95 | int imask = atoi(mask+1); | ||
| 96 | if (masklen < 1 || masklen > 3 || !isdigit(*(mask+1)) || (masklen >= 2 && !isdigit(*(mask+2))) || (masklen == 3 && !isdigit(*(mask+3))) || imask < 0 || imask > 128) { | ||
| 95 | sp_log_err("config", "'%s' isn't a valid network mask.", mask + 1); | 97 | sp_log_err("config", "'%s' isn't a valid network mask.", mask + 1); |
| 96 | return -1; | 98 | return -1; |
| 97 | } | 99 | } |
| 100 | cidr->mask = (uint8_t)imask; | ||
| 98 | 101 | ||
| 99 | ip[mask - ip] = '\0'; // NULL the '/' char | 102 | ip[mask - ip] = '\0'; // NULL the '/' char |
| 100 | 103 | ||
| @@ -113,5 +116,10 @@ int get_ip_and_cidr(char *ip, sp_cidr *cidr) { | |||
| 113 | } | 116 | } |
| 114 | 117 | ||
| 115 | ip[mask - ip] = '/'; | 118 | ip[mask - ip] = '/'; |
| 119 | if (cidr->ip_version < 0) { | ||
| 120 | sp_log_err("cidr_match", "Weird ip (%s) family", ip); | ||
| 121 | return -1; | ||
| 122 | } | ||
| 123 | |||
| 116 | return 0; | 124 | return 0; |
| 117 | } | 125 | } |
diff --git a/src/tests/broken_configuration/broken_conf_invalid_cidr6_too_big.phpt b/src/tests/broken_configuration/broken_conf_invalid_cidr6_too_big.phpt index 47d4a5d..bc353c9 100644 --- a/src/tests/broken_configuration/broken_conf_invalid_cidr6_too_big.phpt +++ b/src/tests/broken_configuration/broken_conf_invalid_cidr6_too_big.phpt | |||
| @@ -3,7 +3,11 @@ Broken configuration, cidr for ipv6 is too big, that will `mod` to 25. | |||
| 3 | (13337%128 = 25) | 3 | (13337%128 = 25) |
| 4 | --SKIPIF-- | 4 | --SKIPIF-- |
| 5 | <?php if (!extension_loaded("snuffleupagus")) print "skip"; ?> | 5 | <?php if (!extension_loaded("snuffleupagus")) print "skip"; ?> |
| 6 | <?php if (PHP_VERSION_ID >= 80000) print "skip"; ?> | ||
| 6 | --INI-- | 7 | --INI-- |
| 7 | sp.configuration_file={PWD}/config/broken_conf_invalid_cidr6_too_big.ini | 8 | sp.configuration_file={PWD}/config/broken_conf_invalid_cidr6_too_big.ini |
| 8 | --FILE-- | 9 | --FILE-- |
| 9 | --EXPECT-- | 10 | --EXPECT-- |
| 11 | |||
| 12 | Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0 | ||
| 13 | Could not startup. \ No newline at end of file | ||
diff --git a/src/tests/broken_configuration_php8/broken_conf_invalid_cidr.phpt b/src/tests/broken_configuration_php8/broken_conf_invalid_cidr.phpt index f66d8b6..7bbf132 100644 --- a/src/tests/broken_configuration_php8/broken_conf_invalid_cidr.phpt +++ b/src/tests/broken_configuration_php8/broken_conf_invalid_cidr.phpt | |||
| @@ -7,8 +7,7 @@ Broken configuration | |||
| 7 | sp.configuration_file={PWD}/config/broken_conf_invalid_cidr.ini | 7 | sp.configuration_file={PWD}/config/broken_conf_invalid_cidr.ini |
| 8 | --FILE-- | 8 | --FILE-- |
| 9 | --EXPECT-- | 9 | --EXPECT-- |
| 10 | |||
| 11 | Fatal error: [snuffleupagus][0.0.0.0][config][log] '42' isn't a valid ipv4 mask. in Unknown on line 0 | 10 | Fatal error: [snuffleupagus][0.0.0.0][config][log] '42' isn't a valid ipv4 mask. in Unknown on line 0 |
| 12 | 11 | ||
| 13 | Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0 | 12 | Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0 |
| 14 | Could not startup. | 13 | Could not startup. \ No newline at end of file |
diff --git a/src/tests/broken_configuration_php8/broken_conf_invalid_cidr6_too_big.phpt b/src/tests/broken_configuration_php8/broken_conf_invalid_cidr6_too_big.phpt index 47d4a5d..07acfb1 100644 --- a/src/tests/broken_configuration_php8/broken_conf_invalid_cidr6_too_big.phpt +++ b/src/tests/broken_configuration_php8/broken_conf_invalid_cidr6_too_big.phpt | |||
| @@ -7,3 +7,7 @@ Broken configuration, cidr for ipv6 is too big, that will `mod` to 25. | |||
| 7 | sp.configuration_file={PWD}/config/broken_conf_invalid_cidr6_too_big.ini | 7 | sp.configuration_file={PWD}/config/broken_conf_invalid_cidr6_too_big.ini |
| 8 | --FILE-- | 8 | --FILE-- |
| 9 | --EXPECT-- | 9 | --EXPECT-- |
| 10 | Fatal error: [snuffleupagus][0.0.0.0][config][log] '13337' isn't a valid network mask. in Unknown on line 0 | ||
| 11 | |||
| 12 | Fatal error: [snuffleupagus][0.0.0.0][config][log] Invalid configuration file in Unknown on line 0 | ||
| 13 | Could not startup. \ No newline at end of file | ||
