diff options
author | Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> | 2012-01-14 15:06:00 +0100 |
---|---|---|
committer | Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> | 2012-01-14 15:06:00 +0100 |
commit | 36e483b303b11890e870eba5a8c798b170244cb3 (patch) | |
tree | 51894eeecb17e00f76405043a2b394e1fdbd00c9 /lib/print.c | |
parent | 64f47a74c303ba53a306e6ad4cd0c7f40a165eea (diff) |
Support hostnames and service names with dash
The square brackets are introduced as an escape mechanism to
enter hostnames or service names with dash in order to avoid
mixing up the dash in the name with the range notation.
Problem reported by Stephen Hemminger and Marc Guardiola.
Diffstat (limited to 'lib/print.c')
-rw-r--r-- | lib/print.c | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/lib/print.c b/lib/print.c index 662baae..f2f848b 100644 --- a/lib/print.c +++ b/lib/print.c @@ -158,13 +158,23 @@ __getnameinfo4(char *buf, unsigned int len, sizeof(saddr), buf, len, NULL, 0, flags); - if (!(flags & NI_NUMERICHOST) && - (err == EAI_AGAIN || (err == 0 && strchr(buf, '-') != NULL))) + if (!(flags & NI_NUMERICHOST) && (err == EAI_AGAIN)) err = getnameinfo((const struct sockaddr *)&saddr, sizeof(saddr), buf, len, NULL, 0, flags | NI_NUMERICHOST); D("getnameinfo err: %i, errno %i", err, errno); + if (err == 0 && strstr(buf, IPSET_RANGE_SEPARATOR) != NULL) { + const char escape[] = IPSET_ESCAPE_START; + /* Escape hostname */ + if (strlen(buf) + 2 > len) { + err = EAI_OVERFLOW; + return -1; + } + memmove(buf + 1, buf, strlen(buf) + 1); + buf[0] = escape[0]; + strcat(buf, IPSET_ESCAPE_END); + } return (err == 0 ? (int)strlen(buf) : (err == EAI_OVERFLOW || err == EAI_SYSTEM) ? (int)len : -1); } @@ -184,13 +194,23 @@ __getnameinfo6(char *buf, unsigned int len, sizeof(saddr), buf, len, NULL, 0, flags); - if (!(flags & NI_NUMERICHOST) && - (err == EAI_AGAIN || (err == 0 && strchr(buf, '-') != NULL))) + if (!(flags & NI_NUMERICHOST) && (err == EAI_AGAIN)) err = getnameinfo((const struct sockaddr *)&saddr, sizeof(saddr), buf, len, NULL, 0, flags | NI_NUMERICHOST); D("getnameinfo err: %i, errno %i", err, errno); + if (err == 0 && strstr(buf, IPSET_RANGE_SEPARATOR) != NULL) { + const char escape[] = IPSET_ESCAPE_START; + /* Escape hostname */ + if (strlen(buf) + 2 > len) { + err = EAI_OVERFLOW; + return -1; + } + memmove(buf + 1, buf, strlen(buf) + 1); + buf[0] = escape[0]; + strcat(buf, IPSET_ESCAPE_END); + } return (err == 0 ? (int)strlen(buf) : (err == EAI_OVERFLOW || err == EAI_SYSTEM) ? (int)len : -1); } |