TAPInterface_root_test.c 4.0 KB

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