about summary refs log tree commit diff
path: root/pkgs/tools/networking/cjdns/rfc5952.patch
blob: 2152d192b53adde6783989cf4a6fcd6cf2d95540 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
diff --git a/admin/angel/cjdroute2.c b/admin/angel/cjdroute2.c
index dfce6c6..77954a7 100644
--- a/admin/angel/cjdroute2.c
+++ b/admin/angel/cjdroute2.c
@@ -80,7 +80,7 @@ static int genAddress(uint8_t addressOut[40],
         if (AddressCalc_addressForPublicKey(address.ip6.bytes, address.key)) {
             Hex_encode(privateKeyHexOut, 65, privateKey, 32);
             Base32_encode(publicKeyBase32Out, 53, address.key, 32);
-            Address_printIp(addressOut, &address);
+            Address_printShortIp(addressOut, &address);
             return 0;
         }
     }
diff --git a/contrib/c/makekeys.c b/contrib/c/makekeys.c
index 3727fff..29582f1 100644
--- a/contrib/c/makekeys.c
+++ b/contrib/c/makekeys.c
@@ -41,7 +41,7 @@ int main(int argc, char** argv)
         if (AddressCalc_addressForPublicKey(ip, publicKey)) {
             Hex_encode(hexPrivateKey, 65, privateKey, 32);
             Base32_encode(publicKeyBase32, 53, publicKey, 32);
-            AddrTools_printIp(printedIp, ip);
+            AddrTools_printShortIp(printedIp, ip);
             printf("%s %s %s.k\n", hexPrivateKey, printedIp, publicKeyBase32);
         }
     }
diff --git a/contrib/c/privatetopublic.c b/contrib/c/privatetopublic.c
index 7f5f967..dc98f1c 100644
--- a/contrib/c/privatetopublic.c
+++ b/contrib/c/privatetopublic.c
@@ -73,7 +73,7 @@ int main(int argc, char** argv)
     AddressCalc_addressForPublicKey(address.ip6.bytes, address.key);
     if (address.ip6.bytes[0] == 0xFC) {
         Base32_encode(publicKeyBase32Out, 53, address.key, 32);
-        Address_printIp(addressOut, &address);
+        Address_printShortIp(addressOut, &address);
         printf(    "Input privkey: %s\n"
             "Matching pubkey: %s.k\n"
             "Resulting address: %s\n"
diff --git a/contrib/c/publictoip6.c b/contrib/c/publictoip6.c
index fc92f7e..99afc4c 100644
--- a/contrib/c/publictoip6.c
+++ b/contrib/c/publictoip6.c
@@ -48,7 +48,7 @@ int main(int argc, char** argv)
     }
 
     uint8_t output[40] = {0};
-    AddrTools_printIp(output, ip6Bytes);
+    AddrTools_printShortIp(output, ip6Bytes);
     printf("%s\n", output);
     return 0;
 }
diff --git a/dht/Address.c b/dht/Address.c
index e4c2dba..ba77cad 100644
--- a/dht/Address.c
+++ b/dht/Address.c
@@ -102,6 +102,12 @@ void Address_printIp(uint8_t output[40], struct Address* addr)
     AddrTools_printIp(output, addr->ip6.bytes);
 }
 
+void Address_printShortIp(uint8_t output[40], struct Address* addr)
+{
+    Address_getPrefix(addr);
+    AddrTools_printShortIp(output, addr->ip6.bytes);
+}
+
 void Address_print(uint8_t output[60], struct Address* addr)
 {
     Address_printIp(output, addr);
diff --git a/dht/Address.h b/dht/Address.h
index 43c6f05..f200b40 100644
--- a/dht/Address.h
+++ b/dht/Address.h
@@ -94,6 +94,8 @@ void Address_forKey(struct Address* out, const uint8_t key[Address_KEY_SIZE]);
 
 void Address_printIp(uint8_t output[40], struct Address* addr);
 
+void Address_printShortIp(uint8_t output[40], struct Address* addr);
+
 void Address_print(uint8_t output[60], struct Address* addr);
 
 String* Address_toString(struct Address* addr, struct Allocator* alloc);
diff --git a/net/Ducttape.c b/net/Ducttape.c
index 84597d0..1813f3f 100644
--- a/net/Ducttape.c
+++ b/net/Ducttape.c
@@ -227,7 +227,7 @@ static inline bool isRouterTraffic(struct Message* message, struct Headers_IP6He
 #define debugHandles(logger, session, message, ...) \
     do {                                                                               \
         uint8_t ip[40];                                                                \
-        AddrTools_printIp(ip, session->ip6);                                           \
+        AddrTools_printIp(ip, session->ip6);                                      \
         Log_debug(logger, "ver[%u] send[%d] recv[%u] ip[%s] " message,                 \
                   session->version,                                                    \
                   Endian_hostToBigEndian32(session->sendHandle_be),                    \
@@ -271,10 +271,10 @@ static inline uint8_t incomingForMe(struct Message* message,
     if (Bits_memcmp(addr.ip6.bytes, dtHeader->ip6Header->sourceAddr, 16)) {
         #ifdef Log_DEBUG
             uint8_t keyAddr[40];
-            Address_printIp(keyAddr, &addr);
+            Address_printShortIp(keyAddr, &addr);
             Bits_memcpyConst(addr.ip6.bytes, dtHeader->ip6Header->sourceAddr, 16);
             uint8_t srcAddr[40];
-            Address_printIp(srcAddr, &addr);
+            Address_printShortIp(srcAddr, &addr);
             Log_debug(context->logger,
                        "DROP packet because source address is not same as key.\n"
                        "    %s source addr\n"
@@ -292,7 +292,7 @@ static inline uint8_t incomingForMe(struct Message* message,
         if (Checksum_udpIp6(dtHeader->ip6Header->sourceAddr, (uint8_t*)uh, message->length)) {
             #ifdef Log_DEBUG
                 uint8_t keyAddr[40];
-                Address_printIp(keyAddr, &addr);
+                Address_printShortIp(keyAddr, &addr);
                 Log_debug(context->logger,
                           "DROP Router packet with incorrect checksum, from [%s]", keyAddr);
             #endif
@@ -708,7 +708,7 @@ static inline int core(struct Message* message,
                     struct Address destination;
                     Bits_memcpyConst(destination.ip6.bytes, ip6Header->destinationAddr, 16);
                     uint8_t ipAddr[40];
-                    Address_printIp(ipAddr, &destination);
+                    Address_printShortIp(ipAddr, &destination);
                     Log_debug(context->logger, "Forwarding data to %s via %s\n", ipAddr, nhAddr);
                 #endif */
             } else {
@@ -723,7 +723,7 @@ static inline int core(struct Message* message,
         struct Address destination;
         Bits_memcpyConst(destination.ip6.bytes, ip6Header->destinationAddr, 16);
         uint8_t ipAddr[40];
-        Address_printIp(ipAddr, &destination);
+        Address_printShortIp(ipAddr, &destination);
         Log_info(context->logger, "DROP message because this node is the closest known "
                                    "node to the destination %s.", ipAddr);
     #endif
diff --git a/test/printIp_test.c b/test/printIp_test.c
new file mode 100644
index 0000000..75d7427
--- /dev/null
+++ b/test/printIp_test.c
@@ -0,0 +1,54 @@
+/* vim: set expandtab ts=4 sw=4: */
+/*
+ * You may redistribute this program and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "crypto/random/Random.h"
+#include "memory/MallocAllocator.h"
+#include "util/AddrTools.h"
+#include "util/Assert.h"
+
+#include <stdio.h>
+
+int main()
+{
+    struct Allocator* alloc = MallocAllocator_new(1<<22);
+    struct Random* rand = Random_new(alloc, NULL, NULL);
+
+    uint8_t ip[16];
+    uint8_t printedIp[40];
+    uint8_t printedShortIp[40];
+    uint8_t ipFromFull[16];
+    uint8_t ipFromShort[16];
+
+    for (int i = 0; i < 1024; ++i) {
+        Random_bytes(rand, ip, 16);
+
+        for (int j = 0; j < 16; j++) {
+            // make the random result have lots of zeros since that's what we're looking for.
+            ip[j] = (ip[j] % 2) ? 0 : ip[j];
+        }
+
+        AddrTools_printIp(printedIp, ip);
+        AddrTools_printShortIp(printedShortIp, ip);
+        //printf("%s\n%s\n\n", printedIp, printedShortIp);
+
+        AddrTools_parseIp(ipFromFull, printedIp);
+        AddrTools_parseIp(ipFromShort, printedShortIp);
+
+        Assert_true(0 == Bits_memcmp(ip, ipFromFull, 16));
+        Assert_true(0 == Bits_memcmp(ipFromFull, ipFromShort, 16));
+    }
+
+    return 0;
+}
diff --git a/util/AddrTools.h b/util/AddrTools.h
index 858ced4..d59544d 100644
--- a/util/AddrTools.h
+++ b/util/AddrTools.h
@@ -143,6 +143,44 @@ static inline void AddrTools_printIp(uint8_t output[40], const uint8_t binIp[16]
     output[39] = '\0';
 }
 
+static inline void AddrTools_printShortIp(uint8_t output[40], const uint8_t binIp[16])
+{
+    /* The chances of hitting :0:0: and breaking
+     * RFC5952 are 1 in (1 / (2^16))^2 * 6.
+     * E. Siler
+     */
+
+    char *p = output;
+    int i = 0;
+    for (; i < 16;) {
+        if ((size_t)p != (size_t)output) {
+            *p++= ':';
+        }
+
+        if (binIp[i] > 0x0F) {
+            Hex_encode(p, 2, &binIp[i++], 1);
+            p += 2;
+        } else if (binIp[i] > 0x00) {
+            *p++ = Hex_encodeLowNibble(binIp[i++]);
+        } else {
+            ++i;
+            if (binIp[i] > 0x0F) {
+                Hex_encode(p, 2, &binIp[i++], 1);
+                p += 2;
+            } else {
+                *p++ = Hex_encodeLowNibble(binIp[i++]);
+            }
+            continue;
+        }
+        Hex_encode(p, 2, &binIp[i++], 1);
+        p += 2;
+    }
+    *p = '\0';
+
+    Assert_true((size_t)p <= ((size_t)output + 40));
+    Assert_true(i <= 16);
+}
+
 /**
  * Parse out an address.
  *
diff --git a/util/Hex.c b/util/Hex.c
index e3e3c4d..b9bce57 100644
--- a/util/Hex.c
+++ b/util/Hex.c
@@ -29,6 +29,8 @@ static const uint8_t numForAscii[] =
     99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
 };
 
+static const char* hexEntities = "0123456789abcdef";
+
 int Hex_encode(uint8_t* output,
                const uint32_t outputLength,
                const uint8_t* in,
@@ -40,8 +42,6 @@ int Hex_encode(uint8_t* output,
         output[inputLength * 2] = '\0';
     }
 
-    static const char* hexEntities = "0123456789abcdef";
-
     for (uint32_t i = 0; i < inputLength; i++) {
         output[i * 2] = hexEntities[in[i] >> 4];
         output[i * 2 + 1] = hexEntities[in[i] & 15];
@@ -88,3 +88,8 @@ int Hex_decode(uint8_t* output,
 
     return length / 2;
 }
+
+uint8_t Hex_encodeLowNibble(const uint8_t nibble)
+{
+    return hexEntities[nibble & 15];
+}
diff --git a/util/Hex.h b/util/Hex.h
index 4570c3e..a12e402 100644
--- a/util/Hex.h
+++ b/util/Hex.h
@@ -41,4 +41,6 @@ bool Hex_isHexEntity(const uint8_t character);
 
 int Hex_decodeByte(const uint8_t highNibble, const uint8_t lowNibble);
 
+uint8_t Hex_encodeLowNibble(const uint8_t nibble);
+
 #endif