ICMP6Generator_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "memory/Allocator.h"
  16. #include "memory/MallocAllocator.h"
  17. #include "crypto/random/Random.h"
  18. #include "interface/ICMP6Generator_pvt.h"
  19. #include "wire/Headers.h"
  20. #include "util/Assert.h"
  21. #include "util/Bits.h"
  22. #include <stdio.h>
  23. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  24. static struct Message* newMessage(struct Allocator* alloc, int messageSize)
  25. {
  26. uint8_t* buff = Allocator_calloc(alloc, messageSize + 64, 1);
  27. return Allocator_clone(alloc, (&(struct Message) {
  28. .bytes = buff + 64,
  29. .length = messageSize,
  30. .padding = 64
  31. }));
  32. }
  33. static void mtuTest(struct Allocator* mainAlloc, struct Random* rand, int messageSize, uint32_t mtu)
  34. {
  35. struct Allocator* alloc = Allocator_child(mainAlloc);
  36. struct Message* msg = newMessage(alloc, messageSize);
  37. uint8_t* sourceAddr = (uint8_t*) "sourceAddress123";
  38. uint8_t* destAddr = (uint8_t*) "destinationAddr1";
  39. // Fill it with predictable data.
  40. for (int i = 0; i < messageSize; i++) {
  41. msg->bytes[i] = i & 0xff;
  42. }
  43. ICMP6Generator_generate(msg, sourceAddr, destAddr, ICMP6Generator_Type_PACKET_TOO_BIG, mtu);
  44. Assert_always(msg->length <= 1280);
  45. struct Headers_IP6Header* header = (struct Headers_IP6Header*) msg->bytes;
  46. Message_shift(msg, -Headers_IP6Header_SIZE);
  47. Assert_always(!Bits_memcmp(sourceAddr, header->sourceAddr, 16));
  48. Assert_always(!Bits_memcmp(destAddr, header->destinationAddr, 16));
  49. Assert_always(Headers_getIpVersion(header) == 6);
  50. Assert_always(header->flowLabelLow_be == 0);
  51. Assert_always(Endian_bigEndianToHost16(header->payloadLength_be) == msg->length);
  52. Assert_always(header->nextHeader == 58); // 58 -> icmp
  53. Assert_always(header->hopLimit == 64);
  54. struct Headers_ICMP6Header* icmp = (struct Headers_ICMP6Header*) msg->bytes;
  55. Message_shift(msg, -Headers_ICMP6Header_SIZE);
  56. Assert_always(icmp->type == 2); // packet too big.
  57. Assert_always(icmp->code == 0);
  58. Assert_always(icmp->additional == Endian_hostToBigEndian32(mtu));
  59. Assert_always(msg->length ==
  60. MIN(messageSize,
  61. ICMP6Generator_MIN_IPV6_MTU
  62. - Headers_IP6Header_SIZE
  63. - Headers_ICMP6Header_SIZE));
  64. uint32_t out = 0;
  65. for (int i = 0; i < msg->length; i++) {
  66. out |= msg->bytes[i] ^ (i & 0xff);
  67. }
  68. Assert_always(!out);
  69. Allocator_free(alloc);
  70. }
  71. static uint8_t messageFromGenerator(struct Message* msg, struct Interface* iface)
  72. {
  73. uint64_t* reassemblyBuff = iface->receiverContext;
  74. struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) msg->bytes;
  75. struct Headers_IP6Fragment* frag = (struct Headers_IP6Fragment*) (&ip6[1]);
  76. int index = Headers_IP6Fragment_getOffset(frag);
  77. Message_shift(msg, -Headers_IP6Header_SIZE);
  78. Assert_always(Endian_bigEndianToHost16(ip6->payloadLength_be) == msg->length);
  79. Message_shift(msg, -Headers_IP6Fragment_SIZE);
  80. Assert_always(msg->length > 0);
  81. Bits_memcpy(&reassemblyBuff[index], msg->bytes, msg->length);
  82. Message_shift(msg, (Headers_IP6Header_SIZE + Headers_IP6Fragment_SIZE));
  83. printf("Got message fragment with index [%d] length [%d] hasMoreFragments [%d]\n",
  84. index, msg->length, Headers_IP6Fragment_hasMoreFragments(frag));
  85. return 0;
  86. }
  87. static void fragTest(struct Allocator* mainAlloc,
  88. struct Random* rand,
  89. int messageSize,
  90. uint32_t mtu)
  91. {
  92. struct Allocator* alloc = Allocator_child(mainAlloc);
  93. struct Message* msg = newMessage(alloc, messageSize);
  94. for (int i = 0; i < msg->length; i++) {
  95. msg->bytes[i] = i & 0xff;
  96. }
  97. struct Headers_IP6Header* ip6 = (struct Headers_IP6Header*) msg->bytes;
  98. Headers_setIpVersion(ip6);
  99. ip6->payloadLength_be = Endian_hostToBigEndian16(messageSize - Headers_IP6Header_SIZE);
  100. struct Headers_IP6Fragment* frag = (struct Headers_IP6Fragment*) (&ip6[1]);
  101. ip6->nextHeader = 44;
  102. Bits_memset(frag, 0, sizeof(frag));
  103. const uint32_t headersSize = Headers_IP6Header_SIZE + Headers_IP6Fragment_SIZE;
  104. const uint32_t reassemblySize = ((messageSize + 7 - headersSize) / 8) * 8;
  105. uint8_t* reassemblyBuff = Allocator_calloc(alloc, reassemblySize, 1);
  106. printf("Allocating [%d] bytes for reassembly.\n", reassemblySize);
  107. struct ICMP6Generator* ig = ICMP6Generator_new(alloc);
  108. ((struct ICMP6Generator_pvt*)ig)->mtu = mtu;
  109. ig->internal.receiveMessage = messageFromGenerator;
  110. ig->internal.receiverContext = reassemblyBuff;
  111. ig->external.sendMessage(msg, &ig->external);
  112. for (int i = 0; i < (int)(messageSize - headersSize); i++) {
  113. Assert_always(reassemblyBuff[i] == ((i + headersSize) & 0xff));
  114. }
  115. Allocator_free(alloc);
  116. }
  117. int main()
  118. {
  119. struct Allocator* alloc = MallocAllocator_new(20000);
  120. struct Random* rand = Random_new(alloc, NULL, NULL);
  121. mtuTest(alloc, rand, 2048, 1500);
  122. mtuTest(alloc, rand, 1500, 1492);
  123. mtuTest(alloc, rand, 1492, 1280);
  124. mtuTest(alloc, rand, 1280, 1024);
  125. mtuTest(alloc, rand, 1492, 1024);
  126. mtuTest(alloc, rand, 1024, 512);
  127. fragTest(alloc, rand, 2048, 1000);
  128. fragTest(alloc, rand, 1400, 1000);
  129. fragTest(alloc, rand, 1300, 500);
  130. fragTest(alloc, rand, 1500, 200);
  131. fragTest(alloc, rand, 1500, 100);
  132. Allocator_free(alloc);
  133. }