TAPInterface_root_test.c 4.0 KB

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