]> sjero.net Git - linphone/commitdiff
Handle IPv6 addresses in parse_hostname_to_addr().
authorGhislain MARY <ghislain.mary@belledonne-communications.com>
Thu, 30 Aug 2012 13:26:28 +0000 (15:26 +0200)
committerGhislain MARY <ghislain.mary@belledonne-communications.com>
Fri, 31 Aug 2012 09:20:09 +0000 (11:20 +0200)
This is needed to correctly handle and IPv6 address set as gateway when
using the nat firewall policy.

coreapi/misc.c

index bf5c53dcce6c37b576542fd0a53695bcaa9fddaf..a22bbc1d9f57706c0050cbf988e28afabb051a75 100644 (file)
@@ -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);