FramingInterface.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/Interface.h"
  16. #include "interface/FramingInterface.h"
  17. #include "interface/InterfaceWrapper.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Identity.h"
  20. #include "wire/Error.h"
  21. struct MessageList;
  22. struct MessageList {
  23. struct Message* msg;
  24. struct MessageList* next;
  25. };
  26. struct FramingInterface_pvt {
  27. struct Interface generic;
  28. struct Interface* const wrapped;
  29. const uint32_t maxMessageSize;
  30. struct Allocator* alloc;
  31. // fields specific to this frame.
  32. uint32_t bytesRemaining;
  33. struct Allocator* frameAlloc;
  34. struct MessageList* frameParts;
  35. union {
  36. uint32_t length_be;
  37. uint8_t bytes[4];
  38. } header;
  39. uint32_t headerIndex;
  40. Identity
  41. };
  42. static struct Message* mergeMessage(struct FramingInterface_pvt* fi, struct Message* last)
  43. {
  44. int length = last->length;
  45. // The only accurate way to get the full length because this last might contain
  46. // the beginning of the next frame.
  47. struct MessageList* part = fi->frameParts;
  48. for (part = fi->frameParts; part; part = part->next) {
  49. length += part->msg->length;
  50. }
  51. int fullLength = length + fi->generic.requiredPadding;
  52. uint8_t* msgBuff = Allocator_malloc(fi->frameAlloc, fullLength);
  53. struct Message* out = Allocator_calloc(fi->frameAlloc, sizeof(struct Message), 1);
  54. out->bytes = msgBuff + fullLength;
  55. out->length = 0;
  56. out->padding = fullLength;
  57. out->capacity = 0;
  58. out->alloc = fi->frameAlloc;
  59. Message_push(out, last->bytes, last->length, NULL);
  60. for (part = fi->frameParts; part; part = part->next) {
  61. Message_push(out, part->msg->bytes, part->msg->length, NULL);
  62. }
  63. Assert_true(fullLength <= out->length);
  64. return out;
  65. }
  66. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  67. {
  68. struct FramingInterface_pvt* fi =
  69. Identity_check((struct FramingInterface_pvt*)iface->receiverContext);
  70. if (fi->bytesRemaining > fi->maxMessageSize) {
  71. return Error_OVERSIZE_MESSAGE;
  72. }
  73. if (fi->frameParts) {
  74. if (fi->bytesRemaining <= (uint32_t)msg->length) {
  75. struct Message* wholeMessage = mergeMessage(fi, msg);
  76. fi->bytesRemaining = 0;
  77. fi->frameParts = NULL;
  78. Assert_true(fi->headerIndex == 0);
  79. struct Allocator* frameAlloc = fi->frameAlloc;
  80. fi->frameAlloc = NULL;
  81. // Run the message through again since it's almost certainly not perfect size.
  82. uint8_t ret = receiveMessage(wholeMessage, iface);
  83. Allocator_free(frameAlloc);
  84. return ret;
  85. }
  86. fi->bytesRemaining -= msg->length;
  87. Allocator_adopt(fi->frameAlloc, msg->alloc);
  88. struct MessageList* parts = Allocator_malloc(fi->frameAlloc, sizeof(struct MessageList));
  89. parts->msg = msg;
  90. parts->next = fi->frameParts;
  91. fi->frameParts = parts;
  92. return Error_NONE;
  93. }
  94. for (;;) {
  95. while (fi->headerIndex < 4) {
  96. if (!msg->length) {
  97. return Error_NONE;
  98. }
  99. fi->header.bytes[fi->headerIndex] = msg->bytes[0];
  100. Message_shift(msg, -1, NULL);
  101. fi->headerIndex++;
  102. }
  103. fi->headerIndex = 0;
  104. fi->bytesRemaining = Endian_bigEndianToHost32(fi->header.length_be);
  105. if (fi->bytesRemaining > fi->maxMessageSize) {
  106. return Error_OVERSIZE_MESSAGE;
  107. }
  108. if (fi->bytesRemaining == (uint32_t)msg->length) {
  109. Interface_receiveMessage(&fi->generic, msg);
  110. fi->bytesRemaining = 0;
  111. return Error_NONE;
  112. } else if (fi->bytesRemaining <= (uint32_t)msg->length) {
  113. struct Message* m = Allocator_clone(msg->alloc, msg);
  114. m->length = fi->bytesRemaining;
  115. Interface_receiveMessage(&fi->generic, m);
  116. Message_shift(msg, -fi->bytesRemaining, NULL);
  117. fi->bytesRemaining = 0;
  118. continue;
  119. } else {
  120. fi->frameAlloc = Allocator_child(fi->alloc);
  121. struct Message* m = Allocator_calloc(fi->frameAlloc, sizeof(struct Message), 1);
  122. m->capacity = m->length = msg->length + 4;
  123. m->bytes = Allocator_malloc(fi->frameAlloc, m->length);
  124. m->alloc = fi->frameAlloc;
  125. Message_shift(m, -m->length, NULL);
  126. Message_push(m, msg->bytes, msg->length, NULL);
  127. Message_push(m, fi->header.bytes, 4, NULL);
  128. fi->bytesRemaining -= msg->length;
  129. fi->frameParts = Allocator_malloc(fi->frameAlloc, sizeof(struct MessageList));
  130. fi->frameParts->msg = m;
  131. fi->frameParts->next = NULL;
  132. }
  133. return Error_NONE;
  134. }
  135. }
  136. static uint8_t sendMessage(struct Message* msg, struct Interface* iface)
  137. {
  138. struct FramingInterface_pvt* fi = Identity_check((struct FramingInterface_pvt*)iface);
  139. int32_t length_be = Endian_hostToBigEndian32((uint32_t)msg->length);
  140. Message_push(msg, &length_be, 4, NULL);
  141. return Interface_sendMessage(fi->wrapped, msg);
  142. }
  143. struct Interface* FramingInterface_new(uint32_t maxMessageSize,
  144. struct Interface* toWrap,
  145. struct Allocator* alloc)
  146. {
  147. struct FramingInterface_pvt* context =
  148. Allocator_clone(alloc, (&(struct FramingInterface_pvt) {
  149. .maxMessageSize = maxMessageSize,
  150. .alloc = alloc,
  151. .wrapped = toWrap
  152. }));
  153. Identity_set(context);
  154. InterfaceWrapper_wrap(toWrap, sendMessage, receiveMessage, &context->generic);
  155. return &context->generic;
  156. }