1
0

FramingIface.c 5.9 KB

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