From 5129e306ab832326e887431c38fc22ebaf6b7433 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Thu, 30 Aug 2012 15:26:28 +0200 Subject: [PATCH] Handle IPv6 addresses in parse_hostname_to_addr(). This is needed to correctly handle and IPv6 address set as gateway when using the nat firewall policy. --- coreapi/misc.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/coreapi/misc.c b/coreapi/misc.c index bf5c53dcc..a22bbc1d9 100644 --- a/coreapi/misc.c +++ b/coreapi/misc.c @@ -408,19 +408,29 @@ static int sendStunRequest(int sock, const struct sockaddr *server, socklen_t ad int parse_hostname_to_addr(const char *server, struct sockaddr_storage *ss, socklen_t *socklen){ struct addrinfo hints,*res=NULL; + int family = PF_INET; + int port_int = 3478; int ret; - const char *port; + char port[6]; char host[NI_MAXHOST]; - char *p; - host[NI_MAXHOST-1]='\0'; - strncpy(host,server,sizeof(host)-1); - p=strchr(host,':'); - if (p) { - *p='\0'; - port=p+1; - }else port="3478"; + char *p1, *p2; + if ((sscanf(server, "[%64[^]]]:%d", host, &port_int) == 2) || (sscanf(server, "[%64[^]]]", host) == 1)) { + family = PF_INET6; + } else { + p1 = strchr(server, ':'); + p2 = strrchr(server, ':'); + if (p1 && p2 && (p1 != p2)) { + family = PF_INET6; + host[NI_MAXHOST-1]='\0'; + strncpy(host, server, sizeof(host) - 1); + } else if (sscanf(server, "%[^:]:%d", host, &port_int) != 2) { + host[NI_MAXHOST-1]='\0'; + strncpy(host, server, sizeof(host) - 1); + } + } + snprintf(port, sizeof(port), "%d", port_int); memset(&hints,0,sizeof(hints)); - hints.ai_family=PF_INET; + hints.ai_family=family; hints.ai_socktype=SOCK_DGRAM; hints.ai_protocol=IPPROTO_UDP; ret=getaddrinfo(host,port,&hints,&res);