TUNInterface_ipv4_root_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "interface/InterfaceController.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 <stdlib.h>
  38. // On loan from the DoD, thanks guys.
  39. static const uint8_t testAddrA[4] = {11, 0, 0, 1};
  40. static const uint8_t testAddrB[4] = {11, 0, 0, 2};
  41. /*
  42. * Setup a UDPAddrInterface and a TUNInterface, test sending traffic between them.
  43. */
  44. static int receivedMessageTUNCount = 0;
  45. static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
  46. {
  47. receivedMessageTUNCount++;
  48. uint16_t ethertype = TUNMessageType_pop(msg, NULL);
  49. if (ethertype != Ethernet_TYPE_IP4) {
  50. printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
  51. return 0;
  52. }
  53. struct Headers_IP4Header* header = (struct Headers_IP4Header*) msg->bytes;
  54. Assert_true(msg->length == Headers_IP4Header_SIZE + Headers_UDPHeader_SIZE + 12);
  55. Assert_true(!Bits_memcmp(header->destAddr, testAddrB, 4));
  56. Assert_true(!Bits_memcmp(header->sourceAddr, testAddrA, 4));
  57. Bits_memcpyConst(header->destAddr, testAddrA, 4);
  58. Bits_memcpyConst(header->sourceAddr, testAddrB, 4);
  59. TUNMessageType_push(msg, ethertype, NULL);
  60. return iface->sendMessage(msg, iface);
  61. }
  62. static uint8_t receiveMessageUDP(struct Message* msg, struct Interface* iface)
  63. {
  64. if (!receivedMessageTUNCount) {
  65. return 0;
  66. }
  67. // Got the message, test successful, tear everything down by freeing the root alloc.
  68. struct Allocator* alloc = iface->receiverContext;
  69. Allocator_free(alloc);
  70. return 0;
  71. }
  72. static void fail(void* ignored)
  73. {
  74. Assert_true(!"timeout");
  75. }
  76. int main(int argc, char** argv)
  77. {
  78. // TODO(cjd): fix TUNConfigurator_addIp4Address() for Illumos, Darwin, BSD.
  79. #if defined(sunos) || defined(darwin) || defined(freebsd)
  80. return 0;
  81. #endif
  82. struct Allocator* alloc = MallocAllocator_new(1<<20);
  83. struct EventBase* base = EventBase_new(alloc);
  84. struct Writer* logWriter = FileWriter_new(stdout, alloc);
  85. struct Log* logger = WriterLog_new(logWriter, alloc);
  86. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET, alloc);
  87. char assignedIfName[TUNInterface_IFNAMSIZ];
  88. struct Interface* tun = TUNInterface_new(NULL, assignedIfName, base, logger, NULL, alloc);
  89. NetDev_addAddress(assignedIfName, addrA, 30, logger, NULL);
  90. struct Sockaddr_storage ss;
  91. Assert_true(!Sockaddr_parse("0.0.0.0", &ss));
  92. struct AddrInterface* udp = UDPAddrInterface_new(base, &ss.addr, alloc, NULL, logger);
  93. struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc);
  94. uint8_t* addr;
  95. Assert_true(4 == Sockaddr_getAddress(dest, &addr));
  96. Bits_memcpy(addr, testAddrB, 4);
  97. struct Message* msg;
  98. Message_STACK(msg, 0, 64);
  99. Message_push(msg, "Hello World", 12, NULL);
  100. Message_push(msg, dest, dest->addrLen, NULL);
  101. udp->generic.receiveMessage = receiveMessageUDP;
  102. udp->generic.receiverContext = alloc;
  103. tun->receiveMessage = receiveMessageTUN;
  104. udp->generic.sendMessage(msg, &udp->generic);
  105. Timeout_setTimeout(fail, NULL, 1000, base, alloc);
  106. EventBase_beginLoop(base);
  107. return 0;
  108. }