From 9b1706c1e1ef1e2ae4de7d6c42f9100f1ba6104d Mon Sep 17 00:00:00 2001 From: Marco d'Itri Date: Sun, 31 Mar 2013 05:18:37 +0200 Subject: [PATCH] Fix parsing of 6to4 addresses The last two bytes of the IPv4 address in 6to4 addresses were not parsed correctly since version 5.0.19. (Fixes #699928.) --- whois.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/whois.c b/whois.c index 890a83b..73b0d36 100644 --- a/whois.c +++ b/whois.c @@ -1058,17 +1058,22 @@ char *convert_6to4(const char *s) new = malloc(sizeof("255.255.255.255")); sprintf(new, "%d.%d.%d.%d", *(ip + 2), *(ip + 3), *(ip + 4), *(ip + 5)); #else + int items; unsigned int a, b; + char c; - if (sscanf(s, "2002:%x::", &a) == 1) { - new = malloc(sizeof("255.255.255.255")); - sprintf(new, "%d.%d.0.0", a >> 8, a & 0xff); - return new; - } + items = sscanf(s, "2002:%x:%x%c", &a, &b, &c); - if (sscanf(s, "2002:%x:%x:", &a, &b) != 2) + if (items <= 0 || items == 2 || (items == 3 && c != ':')) return strdup("0.0.0.0"); + if (items == 1) { + items = sscanf(s, "2002:%x:%c", &a, &c); + if (items != 2 || c != ':') + return strdup("0.0.0.0"); + b = 0; + } + new = malloc(sizeof("255.255.255.255")); sprintf(new, "%d.%d.%d.%d", a >> 8, a & 0xff, b >> 8, b & 0xff); #endif