1
0

threeNodes_test_disabled.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strncpy
  16. #define string_strlen
  17. #include "crypto/Key.h"
  18. #include "io/FileWriter.h"
  19. #include "memory/MallocAllocator.h"
  20. #include "memory/Allocator.h"
  21. #include "util/Base32.h"
  22. #include "util/Checksum.h"
  23. #include "util/platform/libc/string.h"
  24. #include "util/log/WriterLog.h"
  25. #include "test/TestFramework.h"
  26. #include "net/Ducttape_pvt.h"
  27. #include "wire/Headers.h"
  28. #include "wire/Ethernet.h"
  29. #include "interface/tuntap/TUNMessageType.h"
  30. #include <stdio.h>
  31. #define TUNC 3
  32. #define TUNB 2
  33. #define TUNA 1
  34. static uint8_t incomingTunC(struct Message* msg, struct Interface* iface)
  35. {
  36. Assert_always(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  37. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  38. printf("Message from TUN in node C [%s] [%d]\n", msg->bytes, msg->length);
  39. *((int*)iface->senderContext) = TUNC;
  40. return 0;
  41. }
  42. static uint8_t incomingTunB(struct Message* msg, struct Interface* iface)
  43. {
  44. Assert_always(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  45. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  46. printf("Message from TUN in node B [%s]\n", msg->bytes);
  47. *((int*)iface->senderContext) = TUNB;
  48. return 0;
  49. }
  50. static uint8_t incomingTunA(struct Message* msg, struct Interface* iface)
  51. {
  52. Assert_always(TUNMessageType_pop(msg, NULL) == Ethernet_TYPE_IP6);
  53. Message_shift(msg, -Headers_IP6Header_SIZE, NULL);
  54. uint8_t buff[1024];
  55. Hex_encode(buff, 1024, msg->bytes, msg->length);
  56. printf("Message from TUN in node A [%s] [%d] [%s]\n", msg->bytes, msg->length, buff);
  57. *((int*)iface->senderContext) = TUNA;
  58. return 0;
  59. }
  60. struct ThreeNodes
  61. {
  62. struct Interface tunIfC;
  63. struct TestFramework* nodeC;
  64. struct Interface tunIfB;
  65. struct TestFramework* nodeB;
  66. struct Interface tunIfA;
  67. struct TestFramework* nodeA;
  68. int messageFrom;
  69. };
  70. static struct ThreeNodes* setUp(struct Allocator* alloc)
  71. {
  72. struct Writer* logwriter = FileWriter_new(stdout, alloc);
  73. struct Log* logger = WriterLog_new(logwriter, alloc);
  74. struct TestFramework* a =
  75. TestFramework_setUp("\xad\x7e\xa3\x26\xaa\x01\x94\x0a\x25\xbc\x9e\x01\x26\x22\xdb\x69"
  76. "\x4f\xd9\xb4\x17\x7c\xf3\xf8\x91\x16\xf3\xcf\xe8\x5c\x80\xe1\x4a",
  77. alloc, logger);
  78. //"publicKey": "kmzm4w0kj9bswd5qmx74nu7kusv5pj40vcsmp781j6xxgpd59z00.k",
  79. //"ipv6": "fc41:94b5:0925:7ba9:3959:11ab:a006:367a",
  80. struct TestFramework* b =
  81. TestFramework_setUp("\xea\x8d\x34\x04\xa9\x7c\xe4\xf9\xca\x7e\x24\xe6\xf1\x85\xb9\x3f"
  82. "\x01\x37\xb7\xa1\xf5\x2c\xce\xc0\x2c\xae\x03\xf1\x83\x38\x13\x24",
  83. alloc, logger);
  84. // This address was found by brute force for one which falls between A and C without being
  85. // closer in either direction (XOR is bidirectional address space distance)
  86. // ipv6: fc2e:3273:644e:426f:283d:e3c7:c87c:41c1
  87. struct TestFramework* c =
  88. TestFramework_setUp("\xd8\x54\x3e\x70\xb9\xae\x7c\x41\xbc\x18\xa4\x9a\x9c\xee\xca\x9c"
  89. "\xdc\x45\x01\x96\x6b\xbd\x7e\x76\xcf\x3a\x9f\xbc\x12\xed\x8b\xb4",
  90. alloc, logger);
  91. //"publicKey": "vz21tg07061s8v9mckrvgtfds7j2u5lst8cwl6nqhp81njrh5wg0.k",
  92. //"ipv6": "fc1f:5b96:e1c5:625d:afde:2523:a7fa:383a",
  93. Log_debug(a->logger, "Linking A and B");
  94. TestFramework_linkNodes(b, a);
  95. Log_debug(a->logger, "Linking A and B complete");
  96. Log_debug(a->logger, "Linking B and C");
  97. TestFramework_linkNodes(c, b);
  98. Log_debug(a->logger, "Linking B and C complete");
  99. struct ThreeNodes* out = Allocator_malloc(alloc, sizeof(struct ThreeNodes));
  100. Bits_memcpyConst(out, (&(struct ThreeNodes) {
  101. .tunIfC = {
  102. .allocator = alloc,
  103. .sendMessage = incomingTunC,
  104. .senderContext = &out->messageFrom
  105. },
  106. .nodeC = c,
  107. .tunIfB = {
  108. .allocator = alloc,
  109. .sendMessage = incomingTunB,
  110. .senderContext = &out->messageFrom
  111. },
  112. .nodeB = b,
  113. .tunIfA = {
  114. .allocator = alloc,
  115. .sendMessage = incomingTunA,
  116. .senderContext = &out->messageFrom
  117. },
  118. .nodeA = a
  119. }), sizeof(struct ThreeNodes));
  120. Ducttape_setUserInterface(c->ducttape, &out->tunIfC);
  121. Ducttape_setUserInterface(b->ducttape, &out->tunIfB);
  122. Ducttape_setUserInterface(a->ducttape, &out->tunIfA);
  123. Log_debug(a->logger, "Setup complete.");
  124. return out;
  125. }
  126. static void sendMessage(struct ThreeNodes* tn,
  127. char* message,
  128. struct TestFramework* from,
  129. struct TestFramework* to)
  130. {
  131. struct Message* msg;
  132. Message_STACK(msg, 64, 512);
  133. Bits_memcpy(msg->bytes, message, strlen(message) + 1);
  134. msg->length = strlen(message) + 1;
  135. TestFramework_craftIPHeader(msg, from->ip, to->ip);
  136. msg = Message_clone(msg, from->alloc);
  137. struct Interface* fromIf;
  138. if (from == tn->nodeA) {
  139. fromIf = &tn->tunIfA;
  140. } else if (from == tn->nodeB) {
  141. fromIf = &tn->tunIfB;
  142. } else if (from == tn->nodeC) {
  143. fromIf = &tn->tunIfC;
  144. } else {
  145. Assert_always(false);
  146. }
  147. TUNMessageType_push(msg, Ethernet_TYPE_IP6, NULL);
  148. fromIf->receiveMessage(msg, fromIf);
  149. if (to == tn->nodeA) {
  150. Assert_always(tn->messageFrom == TUNA);
  151. } else if (to == tn->nodeB) {
  152. Assert_always(tn->messageFrom == TUNB);
  153. } else if (to == tn->nodeC) {
  154. Assert_always(tn->messageFrom == TUNC);
  155. } else {
  156. Assert_always(false);
  157. }
  158. TestFramework_assertLastMessageUnaltered(tn->nodeA);
  159. TestFramework_assertLastMessageUnaltered(tn->nodeB);
  160. TestFramework_assertLastMessageUnaltered(tn->nodeC);
  161. tn->messageFrom = 0;
  162. }
  163. /** Check if nodes A and C can communicate via B without A knowing that C exists. */
  164. int main()
  165. {
  166. struct Allocator* alloc = MallocAllocator_new(1<<22);
  167. struct ThreeNodes* tn = setUp(alloc);
  168. sendMessage(tn, "Hello World!", tn->nodeA, tn->nodeC);
  169. sendMessage(tn, "Hello cjdns!", tn->nodeC, tn->nodeA);
  170. sendMessage(tn, "send", tn->nodeA, tn->nodeC);
  171. sendMessage(tn, "a", tn->nodeC, tn->nodeA);
  172. sendMessage(tn, "few", tn->nodeA, tn->nodeC);
  173. sendMessage(tn, "packets", tn->nodeC, tn->nodeA);
  174. sendMessage(tn, "to", tn->nodeA, tn->nodeC);
  175. sendMessage(tn, "make", tn->nodeC, tn->nodeA);
  176. sendMessage(tn, "sure", tn->nodeA, tn->nodeC);
  177. sendMessage(tn, "the", tn->nodeC, tn->nodeA);
  178. sendMessage(tn, "cryptoauth", tn->nodeA, tn->nodeC);
  179. sendMessage(tn, "can", tn->nodeC, tn->nodeA);
  180. sendMessage(tn, "establish", tn->nodeA, tn->nodeC);
  181. Allocator_free(alloc);
  182. return 0;
  183. }