IpTunnel_test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_strcpy
  16. #define string_strlen
  17. #include "memory/Allocator.h"
  18. #include "memory/MallocAllocator.h"
  19. #include "io/FileWriter.h"
  20. #include "interface/tuntap/TUNMessageType.h"
  21. #include "util/log/Log.h"
  22. #include "util/log/WriterLog.h"
  23. #include "util/events/EventBase.h"
  24. #include "crypto/random/Random.h"
  25. #include "crypto/AddressCalc.h"
  26. #include "tunnel/IpTunnel.h"
  27. #include "util/platform/libc/string.h"
  28. #include "util/Bits.h"
  29. #include "util/Checksum.h"
  30. #include "wire/Message.h"
  31. #include "wire/Headers.h"
  32. #include "wire/Ethernet.h"
  33. static uint8_t* fakePubKey = (uint8_t*) "abcdefghijklmnopqrstuvwxyz012345";
  34. static uint8_t nodeCjdnsIp6[16];
  35. static uint8_t* fakeIp6ToGive = (uint8_t*) "\xfd\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1";
  36. static int called = 0;
  37. static uint8_t responseWithIpCallback(struct Message* message, struct Interface* iface)
  38. {
  39. struct IpTunnel_PacketInfoHeader* pi = (struct IpTunnel_PacketInfoHeader*) message->bytes;
  40. Assert_always(!Bits_memcmp(nodeCjdnsIp6, pi->nodeIp6Addr, 16));
  41. Assert_always(!Bits_memcmp(fakePubKey, pi->nodeKey, 32));
  42. Message_shift(message, -IpTunnel_PacketInfoHeader_SIZE, NULL);
  43. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->bytes;
  44. Assert_always(Headers_getIpVersion(ip) == 6);
  45. uint16_t length = Endian_bigEndianToHost16(ip->payloadLength_be);
  46. Assert_always(length + Headers_IP6Header_SIZE == message->length);
  47. Assert_always(ip->nextHeader == 17);
  48. Assert_always(Bits_isZero(ip->sourceAddr, 32));
  49. Message_shift(message, -Headers_IP6Header_SIZE, NULL);
  50. struct Headers_UDPHeader* uh = (struct Headers_UDPHeader*) message->bytes;
  51. Assert_always(!Checksum_udpIp6(ip->sourceAddr, message->bytes, length));
  52. Assert_always(uh->srcPort_be == 0);
  53. Assert_always(uh->destPort_be == 0);
  54. Assert_always(Endian_bigEndianToHost16(uh->length_be) + Headers_UDPHeader_SIZE == length);
  55. Message_shift(message, -Headers_UDPHeader_SIZE, NULL);
  56. char* expectedResponse =
  57. "d"
  58. "9:addresses" "d"
  59. "3:ip6" "16:\xfd\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1"
  60. "e"
  61. "4:txid" "4:abcd"
  62. "e";
  63. Assert_always(message->length == (int32_t) strlen(expectedResponse));
  64. Assert_always(!Bits_memcmp(message->bytes, expectedResponse, message->length));
  65. called = 1;
  66. return 0;
  67. }
  68. static uint8_t messageToTun(struct Message* message, struct Interface* iface)
  69. {
  70. Assert_always(TUNMessageType_pop(message, NULL) == Ethernet_TYPE_IP6);
  71. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->bytes;
  72. Assert_always(Headers_getIpVersion(ip) == 6);
  73. uint16_t length = Endian_bigEndianToHost16(ip->payloadLength_be);
  74. Assert_always(length + Headers_IP6Header_SIZE == message->length);
  75. Assert_always(ip->nextHeader == 17);
  76. Assert_always(!Bits_memcmp(ip->sourceAddr, fakeIp6ToGive, 16));
  77. called = 1;
  78. return 0;
  79. }
  80. int main()
  81. {
  82. AddressCalc_addressForPublicKey(nodeCjdnsIp6, fakePubKey);
  83. struct Allocator* alloc = MallocAllocator_new(1<<20);
  84. struct Writer* w = FileWriter_new(stdout, alloc);
  85. struct Log* logger = WriterLog_new(w, alloc);
  86. struct Random* rand = Random_new(alloc, logger, NULL);
  87. struct EventBase* eb = EventBase_new(alloc);
  88. struct IpTunnel* ipTun = IpTunnel_new(logger, eb, alloc, rand, NULL);
  89. struct Sockaddr_storage ip6ToGive;
  90. Sockaddr_parse("fd01:0101:0101:0101:0101:0101:0101:0101", &ip6ToGive);
  91. IpTunnel_allowConnection(fakePubKey, &ip6ToGive.addr, NULL, ipTun);
  92. struct Message* message;
  93. Message_STACK(message, 64, 512);
  94. message->alloc = alloc;
  95. const char* requestForAddresses =
  96. "d"
  97. "1:q" "21:IpTunnel_getAddresses"
  98. "4:txid" "4:abcd"
  99. "e";
  100. strcpy((char*)message->bytes, requestForAddresses);
  101. message->length = strlen(requestForAddresses);
  102. Message_shift(message, Headers_UDPHeader_SIZE, NULL);
  103. struct Headers_UDPHeader* uh = (struct Headers_UDPHeader*) message->bytes;
  104. uh->srcPort_be = 0;
  105. uh->destPort_be = 0;
  106. uh->length_be = Endian_hostToBigEndian16(message->length - Headers_UDPHeader_SIZE);
  107. uint16_t* checksum = &uh->checksum_be;
  108. *checksum = 0;
  109. uint32_t length = message->length;
  110. Message_shift(message, Headers_IP6Header_SIZE, NULL);
  111. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->bytes;
  112. ip->versionClassAndFlowLabel = 0;
  113. ip->flowLabelLow_be = 0;
  114. ip->payloadLength_be = Endian_hostToBigEndian16(length);
  115. ip->nextHeader = 17;
  116. ip->hopLimit = 255;
  117. Bits_memset(ip->sourceAddr, 0, 32);
  118. Headers_setIpVersion(ip);
  119. Message_shift(message, IpTunnel_PacketInfoHeader_SIZE, NULL);
  120. struct IpTunnel_PacketInfoHeader* pi = (struct IpTunnel_PacketInfoHeader*) message->bytes;
  121. Bits_memcpyConst(pi->nodeIp6Addr, nodeCjdnsIp6, 16);
  122. Bits_memcpyConst(pi->nodeKey, fakePubKey, 32);
  123. *checksum = Checksum_udpIp6(ip->sourceAddr, (uint8_t*) uh, length);
  124. ipTun->nodeInterface.receiveMessage = responseWithIpCallback;
  125. ipTun->nodeInterface.sendMessage(message, &ipTun->nodeInterface);
  126. Assert_always(called);
  127. called = 0;
  128. // Now create a message for someone else.
  129. Message_shift(message,
  130. Headers_UDPHeader_SIZE
  131. + Headers_IP6Header_SIZE
  132. + IpTunnel_PacketInfoHeader_SIZE,
  133. NULL);
  134. Bits_memcpyConst(ip->sourceAddr, fakeIp6ToGive, 16);
  135. // This can't be zero.
  136. Bits_memset(ip->destinationAddr, 1, 16);
  137. ipTun->tunInterface.receiveMessage = messageToTun;
  138. ipTun->nodeInterface.sendMessage(message, &ipTun->nodeInterface);
  139. Assert_always(called);
  140. Allocator_free(alloc);
  141. return 0;
  142. }