TUNInterface_ipv4_root_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "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/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 = TUNMessageType_pop(msg, NULL);
  34. if (ethertype != Ethernet_TYPE_IP4) {
  35. Log_debug(tt->log, "Spurious packet with ethertype [%u]\n",
  36. Endian_bigEndianToHost16(ethertype));
  37. return 0;
  38. }
  39. struct Headers_IP4Header* header = (struct Headers_IP4Header*) msg->bytes;
  40. Assert_true(msg->length == 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_memcpyConst(header->destAddr, testAddrA, 4);
  44. Bits_memcpyConst(header->sourceAddr, testAddrB, 4);
  45. TUNMessageType_push(msg, ethertype, NULL);
  46. return Iface_next(&tt->tunIface, msg);
  47. }
  48. int main(int argc, char** argv)
  49. {
  50. // TODO(cjd): fix TUNConfigurator_addIp4Address() for Illumos, Darwin, BSD.
  51. #if defined(sunos) || defined(darwin) || defined(freebsd)
  52. return 0;
  53. #endif
  54. struct Allocator* alloc = MallocAllocator_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 = TUNInterface_new(NULL, assignedIfName, 0, base, logger, NULL, alloc);
  61. NetDev_addAddress(assignedIfName, addrA, 30, logger, NULL);
  62. TUNTools_echoTest(addrA, addrB, receiveMessageTUN, tun, base, logger, alloc);
  63. Allocator_free(alloc);
  64. return 0;
  65. }