diff --git a/src/upstream.c b/src/upstream.c index 327b727..c84bad2 100644 --- a/src/upstream.c +++ b/src/upstream.c @@ -43,6 +43,35 @@ proxy_type_name(proxy_type type) } } +/** + * Parse and process a netmask format (IP/mask), otherwise treat as a domain + */ +static void upstream_handle_site_spec(struct upstream *up, const char *domain) +{ + char *ptr; + + ptr = strchr (domain, '/'); + if (ptr) { + struct in_addr addrstruct; + + *ptr = '\0'; + if (inet_aton (domain, &addrstruct) != 0) { + up->ip = ntohl (addrstruct.s_addr); + *ptr++ = '/'; + + if (strchr (ptr, '.')) { + if (inet_aton (ptr, &addrstruct) != 0) + up->mask = + ntohl (addrstruct.s_addr); + } else { + up->mask = + ~((1 << (32 - atoi (ptr))) - 1); + } + } + } else { + up->domain = safestrdup (domain); + } +} /** * Construct an upstream struct from input data. */ @@ -50,7 +79,6 @@ static struct upstream *upstream_build (const char *host, int port, const char * const char *user, const char *pass, proxy_type type) { - char *ptr; struct upstream *up; up = (struct upstream *) safemalloc (sizeof (struct upstream)); @@ -99,27 +127,7 @@ static struct upstream *upstream_build (const char *host, int port, const char * goto fail; } - ptr = strchr (domain, '/'); - if (ptr) { - struct in_addr addrstruct; - - *ptr = '\0'; - if (inet_aton (domain, &addrstruct) != 0) { - up->ip = ntohl (addrstruct.s_addr); - *ptr++ = '/'; - - if (strchr (ptr, '.')) { - if (inet_aton (ptr, &addrstruct) != 0) - up->mask = - ntohl (addrstruct.s_addr); - } else { - up->mask = - ~((1 << (32 - atoi (ptr))) - 1); - } - } - } else { - up->domain = safestrdup (domain); - } + upstream_handle_site_spec(up, domain); log_message (LOG_INFO, "Added no-upstream for %s", domain); } else {