TUNInterface_ipv4_root_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "interface/tuntap/TUNInterface.h"
  16. #include "interface/tuntap/TUNMessageType.h"
  17. #include "memory/Allocator.h"
  18. #include "memory/MallocAllocator.h"
  19. #include "util/Assert.h"
  20. #include "util/log/Log.h"
  21. #include "util/log/FileWriterLog.h"
  22. #include "util/events/Timeout.h"
  23. #include "wire/Error.h"
  24. #include "wire/Ethernet.h"
  25. #include "wire/Headers.h"
  26. #include "util/platform/netdev/NetDev.h"
  27. #include "test/RootTest.h"
  28. #include "interface/tuntap/test/TUNTools.h"
  29. // On loan from the DoD, thanks guys.
  30. static const uint8_t testAddrA[4] = {11, 0, 0, 1};
  31. static const uint8_t testAddrB[4] = {11, 0, 0, 2};
  32. static Iface_DEFUN receiveMessageTUN(struct Message* msg, struct TUNTools* tt)
  33. {
  34. uint16_t ethertype = Er_assert(TUNMessageType_pop(msg));
  35. if (ethertype != Ethernet_TYPE_IP4) {
  36. Log_debug(tt->log, "Spurious packet with ethertype [%u]\n",
  37. Endian_bigEndianToHost16(ethertype));
  38. return Error(msg, "INVALID");
  39. }
  40. struct Headers_IP4Header* header = (struct Headers_IP4Header*) msg->msgbytes;
  41. Assert_true(Message_getLength(msg) == Headers_IP4Header_SIZE + Headers_UDPHeader_SIZE + 12);
  42. Assert_true(!Bits_memcmp(header->destAddr, testAddrB, 4));
  43. Assert_true(!Bits_memcmp(header->sourceAddr, testAddrA, 4));
  44. Bits_memcpy(header->destAddr, testAddrA, 4);
  45. Bits_memcpy(header->sourceAddr, testAddrB, 4);
  46. Er_assert(TUNMessageType_push(msg, ethertype));
  47. return Iface_next(&tt->tunIface, msg);
  48. }
  49. int main(int argc, char** argv)
  50. {
  51. // TODO(cjd): fix TUNConfigurator_addIp4Address() for Illumos, BSD.
  52. #if defined(sunos) || defined(freebsd)
  53. return 0;
  54. #endif
  55. struct Allocator* alloc = MallocAllocator_new(1<<20);
  56. struct EventBase* base = EventBase_new(alloc);
  57. struct Log* logger = FileWriterLog_new(stdout, alloc);
  58. struct Sockaddr* addrA = Sockaddr_fromBytes(testAddrA, Sockaddr_AF_INET, alloc);
  59. struct Sockaddr* addrB = Sockaddr_fromBytes(testAddrB, Sockaddr_AF_INET, alloc);
  60. char assignedIfName[TUNInterface_IFNAMSIZ];
  61. struct Iface* tun = Er_assert(TUNInterface_new(NULL, assignedIfName, 0, base, logger, alloc));
  62. addrA->flags |= Sockaddr_flags_PREFIX;
  63. addrA->prefix = 30;
  64. Er_assert(NetDev_addAddress(assignedIfName, addrA, logger, alloc));
  65. TUNTools_echoTest(addrA, addrB, receiveMessageTUN, tun, base, logger, alloc);
  66. Allocator_free(alloc);
  67. return 0;
  68. }