TUNInterface_ipv6_root_test.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "admin/testframework/AdminTestFramework.h"
  16. #include "admin/Admin.h"
  17. #include "admin/AdminClient.h"
  18. #include "benc/Dict.h"
  19. #include "benc/String.h"
  20. #include "benc/Int.h"
  21. #include "exception/Jmp.h"
  22. #include "interface/addressable/UDPAddrInterface.h"
  23. #include "interface/tuntap/TUNInterface.h"
  24. #include "interface/tuntap/TUNMessageType.h"
  25. #include "memory/Allocator.h"
  26. #include "memory/MallocAllocator.h"
  27. #include "io/FileWriter.h"
  28. #include "io/Writer.h"
  29. #include "util/Assert.h"
  30. #include "util/log/Log.h"
  31. #include "util/log/WriterLog.h"
  32. #include "util/platform/libc/string.h"
  33. #include "util/events/Timeout.h"
  34. #include "wire/Ethernet.h"
  35. #include "wire/Headers.h"
  36. #include "util/platform/netdev/NetDev.h"
  37. #include "test/RootTest.h"
  38. #include <unistd.h>
  39. #include <stdlib.h>
  40. #ifdef win32
  41. #include <windows.h>
  42. #define sleep(x) Sleep(1000*x)
  43. #endif
  44. static const uint8_t testAddrA[] = {0xfd,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  45. static const uint8_t testAddrB[] = {0xfd,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
  46. /*
  47. * Setup a UDPInterface and a TUNInterface, test sending traffic between them.
  48. */
  49. static int receivedMessageTUNCount = 0;
  50. static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
  51. {
  52. receivedMessageTUNCount++;
  53. uint16_t ethertype = TUNMessageType_pop(msg, NULL);
  54. if (ethertype != Ethernet_TYPE_IP6) {
  55. printf("Spurious packet with ethertype [%04x]\n", Endian_bigEndianToHost16(ethertype));
  56. return 0;
  57. }
  58. struct Headers_IP6Header* header = (struct Headers_IP6Header*) msg->bytes;
  59. if (msg->length != Headers_IP6Header_SIZE + Headers_UDPHeader_SIZE + 12) {
  60. int type = (msg->length >= Headers_IP6Header_SIZE) ? header->nextHeader : -1;
  61. printf("Message of unexpected length [%u] ip6->nextHeader: [%d]\n", msg->length, type);
  62. return 0;
  63. }
  64. Assert_always(!Bits_memcmp(header->destinationAddr, testAddrB, 16));
  65. Assert_always(!Bits_memcmp(header->sourceAddr, testAddrA, 16));
  66. Bits_memcpyConst(header->destinationAddr, testAddrA, 16);
  67. Bits_memcpyConst(header->sourceAddr, testAddrB, 16);
  68. TUNMessageType_push(msg, ethertype, NULL);
  69. return iface->sendMessage(msg, iface);
  70. }
  71. static uint8_t receiveMessageUDP(struct Message* msg, struct Interface* iface)
  72. {
  73. if (!receivedMessageTUNCount) {
  74. return 0;
  75. }
  76. // Got the message, test successful.
  77. struct Allocator* alloc = iface->receiverContext;
  78. Allocator_free(alloc);
  79. return 0;
  80. }
  81. static void fail(void* ignored)
  82. {
  83. Assert_always(!"timeout");
  84. }
  85. static struct AddrInterface* setupUDP(struct EventBase* base,
  86. struct Sockaddr* bindAddr,
  87. struct Allocator* allocator,
  88. struct Log* logger)
  89. {
  90. struct Jmp jmp;
  91. Jmp_try(jmp) {
  92. return UDPAddrInterface_new(base, bindAddr, allocator, &jmp.handler, logger);
  93. } Jmp_catch {
  94. sleep(1);
  95. return NULL;
  96. }
  97. }
  98. int main(int argc, char** argv)
  99. {
  100. struct Allocator* alloc = MallocAllocator_new(1<<20);
  101. struct EventBase* base = EventBase_new(alloc);
  102. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  103. struct Log* logger = WriterLog_new(logWriter, alloc);
  104. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET6, alloc);
  105. char assignedIfName[TUNInterface_IFNAMSIZ];
  106. struct Interface* tun = TUNInterface_new(NULL, assignedIfName, base, logger, NULL, alloc);
  107. NetDev_addAddress(assignedIfName, addrA, 126, logger, NULL);
  108. struct Sockaddr_storage addr;
  109. Assert_always(!Sockaddr_parse("[fd00::1]", &addr));
  110. #ifdef freebsd
  111. // tun is not setup synchronously in bsd but it lets you bind to the tun's
  112. // address anyway.
  113. sleep(1);
  114. #endif
  115. // Mac OSX and BSD do not set up their TUN devices synchronously.
  116. // We'll just keep on trying until this works.
  117. struct AddrInterface* udp = NULL;
  118. for (int i = 0; i < 20; i++) {
  119. if ((udp = setupUDP(base, &addr.addr, alloc, logger))) {
  120. break;
  121. }
  122. }
  123. Assert_always(udp);
  124. struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc);
  125. uint8_t* addrBytes;
  126. Assert_always(16 == Sockaddr_getAddress(dest, &addrBytes));
  127. Bits_memcpy(addrBytes, testAddrB, 16);
  128. struct Message* msg;
  129. Message_STACK(msg, 0, 64);
  130. Message_push(msg, "Hello World", 12, NULL);
  131. Message_push(msg, dest, dest->addrLen, NULL);
  132. udp->generic.receiveMessage = receiveMessageUDP;
  133. udp->generic.receiverContext = alloc;
  134. tun->receiveMessage = receiveMessageTUN;
  135. udp->generic.sendMessage(msg, &udp->generic);
  136. Timeout_setTimeout(fail, NULL, 10000, base, alloc);
  137. EventBase_beginLoop(base);
  138. return 0;
  139. }