TAPWrapper_root_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "interface/tuntap/NDPServer.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/events/Timeout.h"
  33. #include "wire/Ethernet.h"
  34. #include "wire/Headers.h"
  35. #include "util/platform/netdev/NetDev.h"
  36. #include "test/RootTest.h"
  37. #include "interface/tuntap/test/TUNTools.h"
  38. #include "interface/tuntap/TAPWrapper.h"
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. static const uint8_t testAddrA[] = {0xfd,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  42. static const uint8_t testAddrB[] = {0xfd,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
  43. /*
  44. * Setup a UDPInterface and a TUNInterface, test sending traffic between them.
  45. */
  46. static int receivedMessageTUNCount = 0;
  47. static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
  48. {
  49. receivedMessageTUNCount++;
  50. uint16_t ethertype = TUNMessageType_pop(msg, NULL);
  51. if (ethertype != Ethernet_TYPE_IP6) {
  52. printf("Spurious packet with ethertype [%04x]\n", Endian_bigEndianToHost16(ethertype));
  53. return 0;
  54. }
  55. struct Headers_IP6Header* header = (struct Headers_IP6Header*) msg->bytes;
  56. if (msg->length != Headers_IP6Header_SIZE + Headers_UDPHeader_SIZE + 12) {
  57. int type = (msg->length >= Headers_IP6Header_SIZE) ? header->nextHeader : -1;
  58. printf("Message of unexpected length [%u] ip6->nextHeader: [%d]\n", msg->length, type);
  59. return 0;
  60. }
  61. if (Bits_memcmp(header->destinationAddr, testAddrB, 16)) { return 0; }
  62. if (Bits_memcmp(header->sourceAddr, testAddrA, 16)) { return 0; }
  63. Bits_memcpyConst(header->destinationAddr, testAddrA, 16);
  64. Bits_memcpyConst(header->sourceAddr, testAddrB, 16);
  65. TUNMessageType_push(msg, ethertype, NULL);
  66. return iface->sendMessage(msg, iface);
  67. }
  68. static uint8_t receiveMessageUDP(struct Message* msg, struct Interface* iface)
  69. {
  70. if (!receivedMessageTUNCount) {
  71. return 0;
  72. }
  73. // Got the message, test successful.
  74. struct Allocator* alloc = iface->receiverContext;
  75. Allocator_free(alloc);
  76. return 0;
  77. }
  78. static void fail(void* ignored)
  79. {
  80. Assert_true(!"timeout");
  81. }
  82. int main(int argc, char** argv)
  83. {
  84. struct Allocator* alloc = MallocAllocator_new(1<<20);
  85. struct EventBase* base = EventBase_new(alloc);
  86. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  87. struct Log* log = WriterLog_new(logWriter, alloc);
  88. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET6, alloc);
  89. char assignedIfName[TUNInterface_IFNAMSIZ];
  90. struct Interface* tap = TUNInterface_new(NULL, assignedIfName, 1, base, log, NULL, alloc);
  91. struct TAPWrapper* tapWrapper = TAPWrapper_new(tap, log, alloc);
  92. // Now setup the NDP server so the tun will work correctly.
  93. struct NDPServer* ndp = NDPServer_new(&tapWrapper->generic, log, TAPWrapper_LOCAL_MAC, alloc);
  94. ndp->advertisePrefix[0] = 0xfd;
  95. ndp->prefixLen = 8;
  96. struct Interface* tun = &ndp->generic;
  97. NetDev_addAddress(assignedIfName, addrA, 126, log, NULL);
  98. struct Sockaddr_storage addr;
  99. Assert_true(!Sockaddr_parse("[::]", &addr));
  100. struct AddrInterface* udp = TUNTools_setupUDP(base, &addr.addr, alloc, log);
  101. struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc);
  102. uint8_t* addrBytes;
  103. Assert_true(16 == Sockaddr_getAddress(dest, &addrBytes));
  104. Bits_memcpy(addrBytes, testAddrB, 16);
  105. udp->generic.receiveMessage = receiveMessageUDP;
  106. udp->generic.receiverContext = alloc;
  107. tun->receiveMessage = receiveMessageTUN;
  108. TUNTools_sendHelloWorld(udp, dest, base, alloc);
  109. Timeout_setTimeout(fail, NULL, 10000000, base, alloc);
  110. EventBase_beginLoop(base);
  111. return 0;
  112. }