1
0

TUNInterface_ipv6_root_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "interface/addressable/UDPAddrInterface.h"
  22. #include "interface/tuntap/TUNInterface.h"
  23. #include "interface/tuntap/TUNMessageType.h"
  24. #include "memory/Allocator.h"
  25. #include "memory/MallocAllocator.h"
  26. #include "io/FileWriter.h"
  27. #include "io/Writer.h"
  28. #include "util/Assert.h"
  29. #include "util/log/Log.h"
  30. #include "util/log/WriterLog.h"
  31. #include "util/events/Timeout.h"
  32. #include "wire/Ethernet.h"
  33. #include "wire/Headers.h"
  34. #include "util/platform/netdev/NetDev.h"
  35. #include "test/RootTest.h"
  36. #include "interface/tuntap/test/TUNTools.h"
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #ifdef win32
  40. #include <windows.h>
  41. #define sleep(x) Sleep(1000*x)
  42. #endif
  43. static const uint8_t testAddrA[] = {0xfd,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  44. static 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, NULL);
  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_true(!Bits_memcmp(header->destinationAddr, testAddrB, 16));
  64. Assert_true(!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, NULL);
  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. int main(int argc, char** argv)
  85. {
  86. struct Allocator* alloc = MallocAllocator_new(1<<20);
  87. struct EventBase* base = EventBase_new(alloc);
  88. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  89. struct Log* logger = WriterLog_new(logWriter, alloc);
  90. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET6, alloc);
  91. char assignedIfName[TUNInterface_IFNAMSIZ];
  92. struct Interface* tun = TUNInterface_new(NULL, assignedIfName, 0, base, logger, NULL, alloc);
  93. NetDev_addAddress(assignedIfName, addrA, 126, logger, NULL);
  94. struct Sockaddr_storage addr;
  95. Assert_true(!Sockaddr_parse("[fd00::1]", &addr));
  96. struct AddrInterface* udp = TUNTools_setupUDP(base, &addr.addr, alloc, logger);
  97. struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc);
  98. uint8_t* addrBytes;
  99. Assert_true(16 == Sockaddr_getAddress(dest, &addrBytes));
  100. Bits_memcpy(addrBytes, testAddrB, 16);
  101. udp->generic.receiveMessage = receiveMessageUDP;
  102. udp->generic.receiverContext = alloc;
  103. tun->receiveMessage = receiveMessageTUN;
  104. TUNTools_sendHelloWorld(udp, dest, base, alloc);
  105. Timeout_setTimeout(fail, NULL, 10000, base, alloc);
  106. EventBase_beginLoop(base);
  107. return 0;
  108. }