From 81a336cce2e8328eece9089690f0d0e042bee38f Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Mon, 3 Dec 2012 23:14:59 +0100 Subject: [PATCH] Properly support the address format for both IPv4 and IPv6 --- src/transport/tcp.c | 13 +++++++++---- src/transport/udp.c | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/transport/tcp.c b/src/transport/tcp.c index 4c1d116..f60339c 100644 --- a/src/transport/tcp.c +++ b/src/transport/tcp.c @@ -187,21 +187,26 @@ static TCP * _tcp_init(AppTransportPluginHelper * helper, static int _init_address(TCP * tcp, char const * name, int domain) { + char sep = ':'; char * p; char * q; char * r; - long l = -1; + long l; struct addrinfo hints; int res = 0; + /* guess the port separator */ + if((q = strchr(name, ':')) != NULL && strchr(++q, ':') != NULL) + sep = '.'; /* obtain the name */ if((p = strdup(name)) == NULL) return -error_set_code(1, "%s", strerror(errno)); - /* FIXME wrong for IPv6 notation (should be '.' then) */ - if((q = strrchr(p, ':')) != NULL) + /* obtain the port number */ + if((q = strrchr(p, sep)) == NULL) + l = -error_set_code(1, "%s", strerror(EINVAL)); + else { *(q++) = '\0'; - /* obtain the port number */ l = strtol(q, &r, 10); if(q[0] == '\0' || *r != '\0') l = -error_set_code(1, "%s", strerror(EINVAL)); diff --git a/src/transport/udp.c b/src/transport/udp.c index da94fce..2aba471 100644 --- a/src/transport/udp.c +++ b/src/transport/udp.c @@ -146,21 +146,26 @@ static UDP * _udp_init(AppTransportPluginHelper * helper, static int _init_address(UDP * udp, char const * name, int domain) { + char sep = ':'; char * p; char * q; char * r; - long l = -1; + long l; struct addrinfo hints; int res = 0; + /* guess the port separator */ + if((q = strchr(name, ':')) != NULL && strchr(++q, ':') != NULL) + sep = '.'; /* obtain the name */ if((p = strdup(name)) == NULL) return -error_set_code(1, "%s", strerror(errno)); - /* FIXME wrong for IPv6 notation (should be '.' then) */ - if((q = strrchr(p, ':')) != NULL) + /* obtain the port number */ + if((q = strrchr(p, sep)) == NULL) + l = -error_set_code(1, "%s", strerror(EINVAL)); + else { *(q++) = '\0'; - /* obtain the port number */ l = strtol(q, &r, 10); if(q[0] == '\0' || *r != '\0') l = -error_set_code(1, "%s", strerror(EINVAL));