upstream: fix ip/mask calculation for types other than none

the code wrongly processed the site_spec (here: domain) parameter
only when PT_TYPE == PT_NONE.
re-arranged code to process it correctly whenever passed.
additionally the mask is now also applied to the passed subnet/ip,
so a site_spec like 127.0.0.1/8 is converted into 127.0.0.0/8.
also the case where inet_aton fails now produces a proper error
message.

note that the code still doesn't process ipv6 addresses and mask.
to support it, we should use the existing code in acl.c and refactor
it so it can be used from both call sites.

closes #83
closes #165
This commit is contained in:
rofl0r 2020-09-07 16:11:51 +01:00
parent 559faf7957
commit 23b0c84653

View File

@ -81,7 +81,13 @@ static struct upstream *upstream_build (const char *host, int port, const char *
}
if (domain == NULL) {
if (!host || host[0] == '\0' || port < 1) {
if (type == PT_NONE) {
e_nonedomain:;
log_message (LOG_WARNING,
"Nonsense upstream none rule: empty domain");
goto fail;
}
if (!host || !host[0] || port < 1) {
log_message (LOG_WARNING,
"Nonsense upstream rule: invalid host or port");
goto fail;
@ -92,12 +98,18 @@ static struct upstream *upstream_build (const char *host, int port, const char *
log_message (LOG_INFO, "Added upstream %s %s:%d for [default]",
proxy_type_name(type), host, port);
} else if (host == NULL || type == PT_NONE) {
if (!domain || domain[0] == '\0') {
} else {
if (type == PT_NONE) {
if (!domain[0]) goto e_nonedomain;
} else {
if (!host || !host[0] || !domain[0]) {
log_message (LOG_WARNING,
"Nonsense no-upstream rule: empty domain");
"Nonsense upstream rule: invalid parameters");
goto fail;
}
up->host = safestrdup (host);
up->port = port;
}
ptr = strchr (domain, '/');
if (ptr) {
@ -116,24 +128,19 @@ static struct upstream *upstream_build (const char *host, int port, const char *
up->mask =
~((1 << (32 - atoi (ptr))) - 1);
}
}
up->ip = up->ip & up->mask;
} else {
up->domain = safestrdup (domain);
}
log_message (LOG_INFO, "Added no-upstream for %s", domain);
} else {
if (!host || host[0] == '\0' || !domain
|| domain[0] == '\0') {
log_message (LOG_WARNING,
"Nonsense upstream rule: invalid parameters");
"Nonsense upstream rule: failed to parse netmask");
goto fail;
}
up->host = safestrdup (host);
up->port = port;
} else {
up->domain = safestrdup (domain);
}
if (type == PT_NONE)
log_message (LOG_INFO, "Added upstream none for %s", domain);
else
log_message (LOG_INFO, "Added upstream %s %s:%d for %s",
proxy_type_name(type), host, port, domain);
}