Add new v2 encoding which should be simpler to parse
authorMatt Corallo <git@bluematt.me>
Sat, 25 Nov 2023 17:45:16 +0000 (17:45 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 25 Nov 2023 18:56:27 +0000 (18:56 +0000)
split.c

diff --git a/split.c b/split.c
index 8e484d0abe7ec766b3825304af1018a1b6844884..1d70a2cbddc797dd2dac8d6349a8d37419960ce8 100644 (file)
--- a/split.c
+++ b/split.c
@@ -34,6 +34,9 @@ int main(int argc, char* argv[]) {
        size_t linelen = 0;
        getline(&hex, &linelen, stdin);
        size_t len = strlen(hex);
+       // len should include a \n but otherwise be a multiple of header_len
+       assert(len % 2 == 1);
+       assert((len-1)/2 % header_len == 0);
 
        if (start_height < total_height - 1) {
                int prev_chunk_count = (total_height - 1) / 10000;
@@ -60,7 +63,48 @@ int main(int argc, char* argv[]) {
        assert(write(out_fd, hex + ((len / header_len / 2) - 1) * header_len * 2, header_len * 2) == header_len * 2);
        assert(write(out_fd, "\n", 1) == 1);
 
+       // We add one byte for the version and then divide (rounding up) by 14 bytes per IPv6.
+       int addr_count = (header_len + 1 + 13) / 14;
        for (int i = 0; i < len / header_len / 2; i++) {
+               int zttl = ttl - i;
+               if (zttl < 60) { zttl = 60; }
+
+               for (int j = 0; j < addr_count; j++) {
+                       char fmt[7*5];
+                       memset(fmt, 0, sizeof(fmt));
+                       for (int k = 0; k < 7; k++) {
+                               char* target = fmt + k * 5;
+                               int start = j*14*2 + k*4 - 2;
+                               int count = header_len*2 - start;
+                               if (start < 0) {
+                                       start = 0;
+                                       target[0] = '0';
+                                       target[1] = '1';
+                                       target += 2;
+                                       if (count > 2) count = 2;
+                               }
+                               if (count < 0) {
+                                       fmt[k*5] = '0';
+                                       continue;
+                               }
+                               if (count > 4) count = 4;
+                               assert(count % 2 == 0);
+                               memcpy(target, &hex[i * header_len*2 + start], count);
+                               if (count < 4 && start != 0) {
+                                       target[2] = '0';
+                                       target[3] = '0';
+                               }
+                       }
+                       dprintf(out_fd, "v2.%d  %d      IN      AAAA    260%d:%s:%s:%s:%s:%s:%s:%s\n",
+                               start_height + i, zttl, j,
+                               &fmt[0 * 5],
+                               &fmt[1 * 5],
+                               &fmt[2 * 5],
+                               &fmt[3 * 5],
+                               &fmt[4 * 5],
+                               &fmt[5 * 5],
+                               &fmt[6 * 5]);
+               }
                if (header_len == 80) {
                        for (int j = 0; j < 6; j++) {
                                char fmt[7*5];
@@ -82,8 +126,6 @@ int main(int argc, char* argv[]) {
                                memcpy(fmt + 4*5, &hex[i * header_len*2 + 3*4 + offs], 4);
                                memcpy(fmt + 5*5, &hex[i * header_len*2 + 4*4 + offs], 4);
                                memcpy(fmt + 6*5, &hex[i * header_len*2 + 5*4 + offs], 4);
-                               int zttl = ttl - i;
-                               if (zttl < 60) { zttl = 60; }
                                dprintf(out_fd, "%d     %d      IN      AAAA    2001:%d%s:%s:%s:%s:%s:%s:%s\n",
                                        start_height + i, zttl, j,
                                        &fmt[0 * 5],
@@ -116,8 +158,6 @@ int main(int argc, char* argv[]) {
                                        fmt[5*5] = '0';
                                        fmt[6*5] = '0';
                                }
-                               int zttl = ttl - i;
-                               if (zttl < 60) { zttl = 60; }
                                dprintf(out_fd, "%d     %d      IN      AAAA    2001:%d%s:%s:%s:%s:%s:%s:%s\n",
                                        start_height + i, zttl, j,
                                        &fmt[0 * 5],