TUNInterface_ipv6_root_test.c 5.2 KB

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