1
0

TUNInterface_ipv4_root_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define string_strcmp
  16. #define string_strlen
  17. #include "admin/testframework/AdminTestFramework.h"
  18. #include "admin/Admin.h"
  19. #include "admin/AdminClient.h"
  20. #include "benc/Dict.h"
  21. #include "benc/String.h"
  22. #include "benc/Int.h"
  23. #include "interface/addressable/UDPAddrInterface.h"
  24. #include "interface/tuntap/TUNInterface.h"
  25. #include "interface/tuntap/TUNMessageType.h"
  26. #include "memory/Allocator.h"
  27. #include "memory/MallocAllocator.h"
  28. #include "interface/InterfaceController.h"
  29. #include "io/FileWriter.h"
  30. #include "io/Writer.h"
  31. #include "util/Assert.h"
  32. #include "util/log/Log.h"
  33. #include "util/log/WriterLog.h"
  34. #include "util/platform/libc/string.h"
  35. #include "util/events/Timeout.h"
  36. #include "wire/Ethernet.h"
  37. #include "wire/Headers.h"
  38. #include "util/platform/netdev/NetDev.h"
  39. #include <stdlib.h>
  40. // On loan from the DoD, thanks guys.
  41. const uint8_t testAddrA[4] = {11, 0, 0, 1};
  42. const uint8_t testAddrB[4] = {11, 0, 0, 2};
  43. /*
  44. * Setup a UDPAddrInterface 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);
  51. if (ethertype != Ethernet_TYPE_IP4) {
  52. printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
  53. return 0;
  54. }
  55. struct Headers_IP4Header* header = (struct Headers_IP4Header*) msg->bytes;
  56. Assert_always(msg->length == Headers_IP4Header_SIZE + Headers_UDPHeader_SIZE + 12);
  57. Assert_always(!Bits_memcmp(header->destAddr, testAddrB, 4));
  58. Assert_always(!Bits_memcmp(header->sourceAddr, testAddrA, 4));
  59. Bits_memcpyConst(header->destAddr, testAddrA, 4);
  60. Bits_memcpyConst(header->sourceAddr, testAddrB, 4);
  61. TUNMessageType_push(msg, ethertype);
  62. return iface->sendMessage(msg, iface);
  63. }
  64. static uint8_t receiveMessageUDP(struct Message* msg, struct Interface* iface)
  65. {
  66. if (!receivedMessageTUNCount) {
  67. return 0;
  68. }
  69. // Got the message, test successful, tear everything down by freeing the root alloc.
  70. struct Allocator* alloc = iface->receiverContext;
  71. Allocator_free(alloc);
  72. return 0;
  73. }
  74. static void fail(void* ignored)
  75. {
  76. Assert_true(!"timeout");
  77. }
  78. int main(int argc, char** argv)
  79. {
  80. // TODO: fix TUNConfigurator_addIp4Address() for Illumos, Darwin, BSD.
  81. #if defined(Illumos) || defined(Darwin) || defined(FreeBSD) || defined(OpenBSD)
  82. return 0;
  83. #endif
  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* logger = WriterLog_new(logWriter, alloc);
  88. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET, alloc);
  89. char assignedIfName[TUNInterface_IFNAMSIZ];
  90. struct Interface* tun = TUNInterface_new(NULL, assignedIfName, base, logger, NULL, alloc);
  91. NetDev_addAddress(assignedIfName, addrA, 30, logger, NULL);
  92. struct Sockaddr_storage ss;
  93. Assert_true(!Sockaddr_parse("0.0.0.0", &ss));
  94. struct AddrInterface* udp = UDPAddrInterface_new(base, &ss.addr, alloc, NULL, logger);
  95. struct Sockaddr* dest = Sockaddr_clone(udp->addr, alloc);
  96. uint8_t* addr;
  97. Assert_true(4 == Sockaddr_getAddress(dest, &addr));
  98. Bits_memcpy(addr, testAddrB, 4);
  99. struct Message* msg;
  100. Message_STACK(msg, 0, 64);
  101. Message_push(msg, "Hello World", 12);
  102. Message_push(msg, dest, dest->addrLen);
  103. udp->generic.receiveMessage = receiveMessageUDP;
  104. udp->generic.receiverContext = alloc;
  105. tun->receiveMessage = receiveMessageTUN;
  106. udp->generic.sendMessage(msg, &udp->generic);
  107. Timeout_setTimeout(fail, NULL, 1000, base, alloc);
  108. EventBase_beginLoop(base);
  109. }