TUNInterface_ipv4_root_test.c 3.0 KB

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