FramingIface.c 6.2 KB

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