IpTunnel_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "memory/Allocator.h"
  16. #include "memory/MallocAllocator.h"
  17. #include "interface/tuntap/TUNMessageType.h"
  18. #include "util/log/Log.h"
  19. #include "util/log/FileWriterLog.h"
  20. #include "util/events/EventBase.h"
  21. #include "crypto/random/Random.h"
  22. #include "crypto/AddressCalc.h"
  23. #include "tunnel/IpTunnel.h"
  24. #include "util/Bits.h"
  25. #include "util/Checksum.h"
  26. #include "util/CString.h"
  27. #include "wire/DataHeader.h"
  28. #include "wire/Message.h"
  29. #include "wire/Headers.h"
  30. #include "wire/Ethernet.h"
  31. static uint8_t* fakePubKey = (uint8_t*) "abcdefghijklmnopqrstuvwxyz012345";
  32. static uint8_t nodeCjdnsIp6[16];
  33. static uint8_t* fakeIp6ToGive = (uint8_t*) "\xfd\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1";
  34. static int called = 0;
  35. static Iface_DEFUN responseWithIpCallback(struct Message* message, struct Iface* iface)
  36. {
  37. struct RouteHeader* rh = (struct RouteHeader*) message->bytes;
  38. Assert_true(!Bits_memcmp(nodeCjdnsIp6, rh->ip6, 16));
  39. Assert_true(!Bits_memcmp(fakePubKey, rh->publicKey, 32));
  40. Message_shift(message, -(RouteHeader_SIZE + DataHeader_SIZE), NULL);
  41. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->bytes;
  42. Assert_true(Headers_getIpVersion(ip) == 6);
  43. uint16_t length = Endian_bigEndianToHost16(ip->payloadLength_be);
  44. Assert_true(length + Headers_IP6Header_SIZE == message->length);
  45. Assert_true(ip->nextHeader == 17);
  46. Assert_true(Bits_isZero(ip->sourceAddr, 32));
  47. Message_shift(message, -Headers_IP6Header_SIZE, NULL);
  48. struct Headers_UDPHeader* uh = (struct Headers_UDPHeader*) message->bytes;
  49. Assert_true(!Checksum_udpIp6(ip->sourceAddr, message->bytes, length));
  50. Assert_true(uh->srcPort_be == 0);
  51. Assert_true(uh->destPort_be == 0);
  52. Assert_true(Endian_bigEndianToHost16(uh->length_be) + Headers_UDPHeader_SIZE == length);
  53. Message_shift(message, -Headers_UDPHeader_SIZE, NULL);
  54. // We can't check that the message is an exact match because the padding depends on the
  55. // alignment of the output but we can make sure the right content is there...
  56. // Message should start with "d0000" (with some number of zeros)
  57. char* expectedResponse =
  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. "9:ip6Prefix" "i0e"
  61. "e"
  62. "4:txid" "4:abcd"
  63. "e";
  64. Assert_true(message->length >= (int32_t) CString_strlen(expectedResponse));
  65. Assert_true(CString_strstr(message->bytes, expectedResponse));
  66. called |= 2;
  67. return 0;
  68. }
  69. static Iface_DEFUN messageToTun(struct Message* message, struct Iface* iface)
  70. {
  71. Assert_true(TUNMessageType_pop(message, NULL) == Ethernet_TYPE_IP6);
  72. struct Headers_IP6Header* ip = (struct Headers_IP6Header*) message->bytes;
  73. Assert_true(Headers_getIpVersion(ip) == 6);
  74. uint16_t length = Endian_bigEndianToHost16(ip->payloadLength_be);
  75. Assert_true(length + Headers_IP6Header_SIZE == message->length);
  76. Assert_true(ip->nextHeader == 17);
  77. Assert_true(!Bits_memcmp(ip->sourceAddr, fakeIp6ToGive, 16));
  78. called |= 1;
  79. return 0;
  80. }
  81. int main()
  82. {
  83. AddressCalc_addressForPublicKey(nodeCjdnsIp6, fakePubKey);
  84. struct Allocator* alloc = MallocAllocator_new(1<<20);
  85. struct Log* logger = FileWriterLog_new(stdout, 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);
  89. struct Sockaddr_storage ip6ToGive;
  90. Sockaddr_parse("fd01:0101:0101:0101:0101:0101:0101:0101", &ip6ToGive);
  91. IpTunnel_allowConnection(fakePubKey, &ip6ToGive.addr, 0, NULL, 0, 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. CString_strcpy((char*)message->bytes, requestForAddresses);
  101. message->length = CString_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, RouteHeader_SIZE + DataHeader_SIZE, NULL);
  120. struct RouteHeader* rh = (struct RouteHeader*) message->bytes;
  121. struct DataHeader* dh = (struct DataHeader*) &rh[1];
  122. Bits_memset(rh, 0, RouteHeader_SIZE + DataHeader_SIZE);
  123. Bits_memcpyConst(rh->ip6, nodeCjdnsIp6, 16);
  124. Bits_memcpyConst(rh->publicKey, fakePubKey, 32);
  125. DataHeader_setContentType(dh, ContentType_IPTUN);
  126. *checksum = Checksum_udpIp6(ip->sourceAddr, (uint8_t*) uh, length);
  127. int origCap = message->capacity;
  128. int origLen = message->length;
  129. struct Iface nodeIface = { .send = responseWithIpCallback };
  130. Iface_plumb(&nodeIface, &ipTun->nodeInterface);
  131. struct Iface tunIface = { .send = messageToTun };
  132. Iface_plumb(&tunIface, &ipTun->tunInterface);
  133. Iface_send(&nodeIface, message);
  134. Assert_true(called == 2);
  135. called = 0;
  136. // This is a hack, reusing the message will cause breakage if IpTunnel is refactored.
  137. Message_reset(message);
  138. Message_shift(message, origCap, NULL);
  139. message->length = origLen;
  140. Bits_memcpyConst(ip->sourceAddr, fakeIp6ToGive, 16);
  141. // This can't be zero.
  142. Bits_memset(ip->destinationAddr, 1, 16);
  143. Iface_send(&nodeIface, message);
  144. Assert_true(called == 1);
  145. Allocator_free(alloc);
  146. return 0;
  147. }