]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
correcting printing and parsing of IPv6 addresses.
authorNikos Mavrogiannopoulos <nmav@crystal.(none)>
Mon, 29 Sep 2008 12:08:02 +0000 (15:08 +0300)
committerNikos Mavrogiannopoulos <nmav@crystal.(none)>
Mon, 29 Sep 2008 12:08:02 +0000 (15:08 +0300)
lib/x509/output.c
src/certtool-cfg.c

index 41246f7f6d872efaa34fe6777408a1fea1284633..a0f0181db638a72401befea886cc22d0049452a4 100644 (file)
@@ -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;
       }
       
index d6249543e9bbd5d3e67381bd0b36517fa6c635e9..796dc7e0f211090eba0195039d45ea762982a075 100644 (file)
@@ -722,26 +722,31 @@ get_tls_server_status (void)
     }
 }
 
-/* avoid depending on in6addr etc. 
- *
- * ip must be of size 16 or more.
- */
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+/* 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;    
   }