PacketProtoDecoder.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @file PacketProtoDecoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/debug.h>
  32. #include <misc/byteorder.h>
  33. #include <misc/minmax.h>
  34. #include <base/BLog.h>
  35. #include <flow/PacketProtoDecoder.h>
  36. #include <generated/blog_channel_PacketProtoDecoder.h>
  37. static void process_data (PacketProtoDecoder *enc);
  38. static void input_handler_done (PacketProtoDecoder *enc, int data_len);
  39. static void output_handler_done (PacketProtoDecoder *enc);
  40. void process_data (PacketProtoDecoder *enc)
  41. {
  42. int was_error = 0;
  43. do {
  44. uint8_t *data = enc->buf + enc->buf_start;
  45. int left = enc->buf_used;
  46. // check if header was received
  47. if (left < sizeof(struct packetproto_header)) {
  48. break;
  49. }
  50. struct packetproto_header header;
  51. memcpy(&header, data, sizeof(header));
  52. data += sizeof(struct packetproto_header);
  53. left -= sizeof(struct packetproto_header);
  54. int data_len = ltoh16(header.len);
  55. // check data length
  56. if (data_len > enc->output_mtu) {
  57. BLog(BLOG_NOTICE, "error: packet too large");
  58. was_error = 1;
  59. break;
  60. }
  61. // check if whole packet was received
  62. if (left < data_len) {
  63. break;
  64. }
  65. // update buffer
  66. enc->buf_start += sizeof(struct packetproto_header) + data_len;
  67. enc->buf_used -= sizeof(struct packetproto_header) + data_len;
  68. // submit packet
  69. PacketPassInterface_Sender_Send(enc->output, data, data_len);
  70. return;
  71. } while (0);
  72. if (was_error) {
  73. // reset buffer
  74. enc->buf_start = 0;
  75. enc->buf_used = 0;
  76. } else {
  77. // if we reached the end of the buffer, wrap around to allow more data to be received
  78. if (enc->buf_start + enc->buf_used == enc->buf_size) {
  79. memmove(enc->buf, enc->buf + enc->buf_start, enc->buf_used);
  80. enc->buf_start = 0;
  81. }
  82. }
  83. // receive data
  84. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf + (enc->buf_start + enc->buf_used), enc->buf_size - (enc->buf_start + enc->buf_used));
  85. // if we had error, report it
  86. if (was_error) {
  87. enc->handler_error(enc->user);
  88. return;
  89. }
  90. }
  91. static void input_handler_done (PacketProtoDecoder *enc, int data_len)
  92. {
  93. ASSERT(data_len > 0)
  94. ASSERT(data_len <= enc->buf_size - (enc->buf_start + enc->buf_used))
  95. DebugObject_Access(&enc->d_obj);
  96. // update buffer
  97. enc->buf_used += data_len;
  98. // process data
  99. process_data(enc);
  100. return;
  101. }
  102. void output_handler_done (PacketProtoDecoder *enc)
  103. {
  104. DebugObject_Access(&enc->d_obj);
  105. // process data
  106. process_data(enc);
  107. return;
  108. }
  109. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg, void *user, PacketProtoDecoder_handler_error handler_error)
  110. {
  111. // init arguments
  112. enc->input = input;
  113. enc->output = output;
  114. enc->user = user;
  115. enc->handler_error = handler_error;
  116. // init input
  117. StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);
  118. // init output
  119. PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
  120. // set output MTU, limit by maximum payload size
  121. enc->output_mtu = bmin_int(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
  122. // init buffer state
  123. enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);
  124. enc->buf_start = 0;
  125. enc->buf_used = 0;
  126. // allocate buffer
  127. if (!(enc->buf = (uint8_t *)malloc(enc->buf_size))) {
  128. goto fail0;
  129. }
  130. // start receiving
  131. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf, enc->buf_size);
  132. DebugObject_Init(&enc->d_obj);
  133. return 1;
  134. fail0:
  135. return 0;
  136. }
  137. void PacketProtoDecoder_Free (PacketProtoDecoder *enc)
  138. {
  139. DebugObject_Free(&enc->d_obj);
  140. // free buffer
  141. free(enc->buf);
  142. }
  143. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc)
  144. {
  145. DebugObject_Access(&enc->d_obj);
  146. enc->buf_start += enc->buf_used;
  147. enc->buf_used = 0;
  148. }