TAPInterface_root_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #if 1
  16. int main(int argc, char** argv)
  17. {
  18. return 0;
  19. }
  20. #else
  21. #include "crypto/AddressCalc.h"
  22. #include "interface/tuntap/windows/NDPServer.h"
  23. #include "exception/Except.h"
  24. #include "memory/Allocator.h"
  25. #include "util/events/EventBase.h"
  26. #include "util/log/Log.h"
  27. #include "util/log/FileWriterLog.h"
  28. #include "util/Hex.h"
  29. #include "util/events/Timeout.h"
  30. #include "util/platform/Sockaddr.h"
  31. #include "util/platform/netdev/NetDev.h"
  32. static uint8_t receiveMessage(Message_t* msg, struct Iface* iface)
  33. {
  34. struct Allocator* alloc = iface->receiverContext;
  35. if (Message_getLength(msg) < 20) {
  36. printf("runt\n");
  37. return 0;
  38. }
  39. // ethernet padding.
  40. Er_assert(Message_eshift(msg, -2));
  41. uint8_t from[13];
  42. uint8_t to[13];
  43. Hex_encode(from, 13, msg->bytes, 6);
  44. Er_assert(Message_eshift(msg, -6));
  45. Hex_encode(to, 13, msg->bytes, 6);
  46. Er_assert(Message_eshift(msg, -6));
  47. uint8_t type[5];
  48. Hex_encode(type, 5, msg->bytes, 2);
  49. Er_assert(Message_eshift(msg, -2));
  50. int subsubtype = -1;
  51. int subtype = -1;
  52. // int typeCode = -1;
  53. if (!Bits_memcmp(type, "86dd", 4)) {
  54. Bits_memcpy(type, "ipv6", 5);
  55. subtype = msg->bytes[6];
  56. // typeCode = 6;
  57. if (subtype == 58) {
  58. subsubtype = msg->bytes[40];
  59. }
  60. } else if (!Bits_memcmp(type, "0800", 4)) {
  61. return 0;
  62. Bits_memcpy(type, "ipv4", 5);
  63. subtype = msg->bytes[9];
  64. // typeCode = 4;
  65. } else {
  66. return 0;
  67. }
  68. // 6000000000183aff0000000000000000000000000000000fff0200000000000000000001ff000018 870
  69. //6000000000201101fd000000000000000000000000000001ff020000000000000000000000010003 eee914...
  70. //6000000000083aff00000000000000000000000000000000ff020000000000000000000000000002 85007b
  71. //6000000000203aff fd000000000000000000000000000001 ff0200000000000000000001ff000002 8700.
  72. int len = Message_getLength(msg) * 2 + 1;
  73. uint8_t* buff = Allocator_malloc(alloc, len + 2);
  74. Hex_encode(buff, len, msg->bytes, Message_getLength(msg));
  75. /* if (typeCode == 6 && len > 86) {
  76. Bits_memmove(&buff[82], &buff[81], len - 81);
  77. Bits_memmove(&buff[49], &buff[48], len - 48);
  78. Bits_memmove(&buff[17], &buff[16], len - 16);
  79. buff[80] = buff[48] = buff[16] = ' ';
  80. }*/
  81. if (Message_getLength(msg) > 45) {
  82. Bits_memcpy(buff+86, "...", 4);
  83. }
  84. printf("[%s] [%s] [%s] [%02d] [%03d] [%s]\n", to, from, type, subtype, subsubtype, buff);
  85. return 0;
  86. }
  87. static void fail(void* vAlloc)
  88. {
  89. printf("Test failed\n");
  90. struct Allocator* alloc = vAlloc;
  91. Allocator_free(alloc);
  92. }
  93. int main(int argc, char** argv)
  94. {
  95. printf("init test");
  96. struct Allocator* alloc = Allocator_new(1<<20);
  97. struct Log* logger = FileWriterLog_new(stdout, alloc);
  98. EventBase_t* base = EventBase_new(alloc);
  99. char* ifName;
  100. struct Iface* iface = TAPInterface_new(NULL, &ifName, NULL, logger, base, alloc);
  101. struct NDPServer* ndp = NDPServer_new(iface, alloc);
  102. ndp->generic.receiveMessage = receiveMessage;
  103. ndp->generic.receiverContext = alloc;
  104. ndp->advertisePrefix[0] = 0xfd;
  105. ndp->prefixLen = AddressCalc_ADDRESS_PREFIX_BITS;
  106. struct Sockaddr_storage ss;
  107. Assert_true(!Sockaddr_parse("fd00::1", &ss));
  108. NetDev_addAddress(ifName, &ss.addr, AddressCalc_ADDRESS_PREFIX_BITS, logger, NULL);
  109. Timeout_setTimeout(fail, alloc, 10000, base, alloc);
  110. EventBase_beginLoop(base);
  111. printf("Test ended\n");
  112. return 0;
  113. }
  114. #endif