From: Nikos Mavrogiannopoulos Date: Mon, 29 Sep 2008 12:08:02 +0000 (+0300) Subject: correcting printing and parsing of IPv6 addresses. X-Git-Tag: gnutls_2_6_0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c357933a30801a82e484bed8fbc4bd5b2b34d5e0;p=thirdparty%2Fgnutls.git correcting printing and parsing of IPv6 addresses. --- diff --git a/lib/x509/output.c b/lib/x509/output.c index 41246f7f6d..a0f0181db6 100644 --- a/lib/x509/output.c +++ b/lib/x509/output.c @@ -123,11 +123,11 @@ uint8_t* ip; sprintf(string, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); break; case 16: - sprintf(string, "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x", - ip[0], ip[1], ip[2], ip[3], - ip[4], ip[5], ip[6], ip[7], - ip[8], ip[9], ip[10], ip[11], - ip[12], ip[13], ip[14], ip[15]); + sprintf(string, "%x:%x:%x:%x:%x:%x:%x:%x", + (ip[0] << 8) | ip[1], (ip[2] << 8) | ip[3], + (ip[4] << 8) | ip[5], (ip[6] <<8) | ip[7], + (ip[8] << 8) | ip[9], (ip[10] << 8) | ip[11], + (ip[12] << 8)| ip[13], (ip[14] << 8) | ip[15]); break; } diff --git a/src/certtool-cfg.c b/src/certtool-cfg.c index d6249543e9..796dc7e0f2 100644 --- a/src/certtool-cfg.c +++ b/src/certtool-cfg.c @@ -722,26 +722,31 @@ get_tls_server_status (void) } } -/* avoid depending on in6addr etc. - * - * ip must be of size 16 or more. - */ +#include +#include +#include + +/* convert a printable IP to binary */ static int string_to_ip( unsigned char *ip, const char * str) { int len = strlen( str); int ret; - - if ( len > 16) { /* IPv6 */ - ret = sscanf( str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", - &ip[0], &ip[1], &ip[2], &ip[3], &ip[4], &ip[5], &ip[6], - &ip[7], &ip[8], &ip[9], &ip[10], &ip[11], &ip[12], &ip[13], - &ip[14], &ip[15]); - if (ret <= 0) return 0; - + + if ( strchr(str, ':') != NULL || len > 16) { /* IPv6 */ + ret = inet_pton(AF_INET6, str, ip); + if (ret <= 0) { + fprintf(stderr, "Error in IPv6 address %s\n", str); + exit(1); + } + + /* To be done */ return 16; } else { /* IPv4 */ - ret = sscanf( str, "%hhu.%hhu.%hhu.%hhu", &ip[0], &ip[1], &ip[2], &ip[3]); - if (ret <= 0) return 0; + ret = inet_pton(AF_INET, str, ip); + if (ret <= 0) { + fprintf(stderr, "Error in IPv4 address %s\n", str); + exit(1); + } return 4; }