Enclose downstream IPv6 numeric address in hostport with square blackets

This commit is contained in:
Tatsuhiro Tsujikawa 2012-06-14 23:01:47 +09:00
parent 5236394c1c
commit 9d84db230d
1 changed files with 21 additions and 5 deletions

View File

@ -60,6 +60,14 @@ void ssl_acceptcb(evconnlistener *listener, int fd,
} }
} // namespace } // namespace
namespace {
bool is_ipv6_numeric_addr(const char *host)
{
uint8_t dst[16];
return inet_pton(AF_INET6, host, dst) == 1;
}
} // namespace
namespace { namespace {
int cache_downstream_host_address() int cache_downstream_host_address()
{ {
@ -421,14 +429,22 @@ int main(int argc, char **argv)
mod_config()->private_key_file = argv[optind++]; mod_config()->private_key_file = argv[optind++];
mod_config()->cert_file = argv[optind++]; mod_config()->cert_file = argv[optind++];
char hostport[NI_MAXHOST]; char hostport[NI_MAXHOST+16];
bool downstream_ipv6_addr =
is_ipv6_numeric_addr(get_config()->downstream_host);
if(get_config()->downstream_port == 80) { if(get_config()->downstream_port == 80) {
mod_config()->downstream_hostport = get_config()->downstream_host; snprintf(hostport, sizeof(hostport), "%s%s%s",
downstream_ipv6_addr ? "[" : "",
get_config()->downstream_host,
downstream_ipv6_addr ? "]" : "");
} else { } else {
snprintf(hostport, sizeof(hostport), "%s:%u", snprintf(hostport, sizeof(hostport), "%s%s%s:%u",
get_config()->downstream_host, get_config()->downstream_port); downstream_ipv6_addr ? "[" : "",
mod_config()->downstream_hostport = hostport; get_config()->downstream_host,
downstream_ipv6_addr ? "]" : "",
get_config()->downstream_port);
} }
mod_config()->downstream_hostport = hostport;
if(cache_downstream_host_address() == -1) { if(cache_downstream_host_address() == -1) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);