From 999bf8d5d61a8d87f4f4cc53e132b095ac707bec Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 22 Sep 2023 01:20:52 -0300 Subject: [PATCH 1/2] Address 2.2.1 of RFC 4291 Treat `2001:DB8:0:0:8:800:200C:417A` as valid. --- src/isinetaddr6.c | 22 +++++++--------------- test/isinetaddr6_test.c | 5 ++++- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/isinetaddr6.c b/src/isinetaddr6.c index d623a3d..b5ac714 100644 --- a/src/isinetaddr6.c +++ b/src/isinetaddr6.c @@ -30,28 +30,20 @@ isinetaddr6(const char *str) char new_str[MAX_STRLEN]; return isinetaddr6(expand(str, len, new_str, i)); } else if (str[i] == SEP) { - if (digitlen < MAX_DIGITLEN) { - return 0; - } else { - digitlen = 0; - hextets++; - } + digitlen = 0; } else if (isxdigit(str[i])) { - if (digitlen == MAX_DIGITLEN) { + digitlen++; + hexdigits++; + if (digitlen > MAX_DIGITLEN) { return 0; - } else { - digitlen++; - hexdigits++; + } else if (str[i-1] == SEP) { + hextets++; } } else { return 0; } } - if (hextets == MAX_HEXTETS) { - return hexdigits <= MAX_HEXDIGITS && digitlen == MAX_DIGITLEN; - } else { - return 0; - } + return hextets == MAX_HEXTETS && hexdigits <= MAX_HEXDIGITS; } static int diff --git a/test/isinetaddr6_test.c b/test/isinetaddr6_test.c index 7e4fe11..1781b49 100644 --- a/test/isinetaddr6_test.c +++ b/test/isinetaddr6_test.c @@ -12,6 +12,9 @@ const char *valid[] = { "1234:5678:9abc:def0:face:face:b00c:0ffe", "2001:19f0:5401:0000:0000:ffff:1e61:face", "dead:beef:cafe:babe:affe:8a2e:0370:7334", + "2001:DB8:0:0:8:800:200C:417A", + "0000:0000:0000:0000:0000:0000:0000:0", + "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", /* valid IPv6 (double colon) */ "fe80::c001:a0ff:fe12:3456", @@ -34,8 +37,8 @@ const char *valid[] = { const char *invalid[] = { /* invalid IPv6 (single colon) */ "0000:0000:0000:0000:0000:0000:0000:000Z", - "0000:0000:0000:0000:0000:0000:0000:0", "0000:0000:0000:0000:0000:0000:0000:", + "2001:DB8:0:Z:8:800:200C:417A", /* invalid IPv6 (double colon) */ ":::", "2001:::1", "2001:::1::", From e7745f086a7eb9e44f6008cbc3deb2d346af9823 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 22 Sep 2023 12:55:40 -0300 Subject: [PATCH 2/2] Add testcases from RFC 4291 --- test/isinetaddr6_test.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/isinetaddr6_test.c b/test/isinetaddr6_test.c index 1781b49..9b81f89 100644 --- a/test/isinetaddr6_test.c +++ b/test/isinetaddr6_test.c @@ -5,6 +5,7 @@ const char *valid[] = { /* valid IPv6 (single colon)*/ + "2001:DB80:0000:0000:0008:800:200C:417A", "0000:0000:0000:0000:0000:0000:0000:0001", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "fe80:0000:0000:0000:0202:b3ff:fe1e:8329", @@ -15,6 +16,10 @@ const char *valid[] = { "2001:DB8:0:0:8:800:200C:417A", "0000:0000:0000:0000:0000:0000:0000:0", "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", + "0:0:0:0:0:0:0:1", + "FF01:0:0:0:0:0:0:101", + "0:0:0:0:0:0:0:0", + "2001:DB8:0:0:8:800:200C:417A", /* valid IPv6 (double colon) */ "fe80::c001:a0ff:fe12:3456", @@ -28,6 +33,7 @@ const char *valid[] = { "0000:0000:0000:0000:0000:0000:0000:00::", "2001:0db8:85a3:0000:0000::8a2e:0370:7334", "2001::5", + "FF01::101", "::ffff:192.168.2.1", "::FFFF:192.168.2.1", "::",