PacketProtoDecoder.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = (struct packetproto_header *)data;
  51. data += sizeof(struct packetproto_header);
  52. left -= sizeof(struct packetproto_header);
  53. int data_len = ltoh16(header->len);
  54. // check data length
  55. if (data_len > enc->output_mtu) {
  56. BLog(BLOG_NOTICE, "error: packet too large");
  57. was_error = 1;
  58. break;
  59. }
  60. // check if whole packet was received
  61. if (left < data_len) {
  62. break;
  63. }
  64. // update buffer
  65. enc->buf_start += sizeof(struct packetproto_header) + data_len;
  66. enc->buf_used -= sizeof(struct packetproto_header) + data_len;
  67. // submit packet
  68. PacketPassInterface_Sender_Send(enc->output, data, data_len);
  69. return;
  70. } while (0);
  71. if (was_error) {
  72. // reset buffer
  73. enc->buf_start = 0;
  74. enc->buf_used = 0;
  75. } else {
  76. // if we reached the end of the buffer, wrap around to allow more data to be received
  77. if (enc->buf_start + enc->buf_used == enc->buf_size) {
  78. memmove(enc->buf, enc->buf + enc->buf_start, enc->buf_used);
  79. enc->buf_start = 0;
  80. }
  81. }
  82. // receive data
  83. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf + (enc->buf_start + enc->buf_used), enc->buf_size - (enc->buf_start + enc->buf_used));
  84. // if we had error, report it
  85. if (was_error) {
  86. enc->handler_error(enc->user);
  87. return;
  88. }
  89. }
  90. static void input_handler_done (PacketProtoDecoder *enc, int data_len)
  91. {
  92. ASSERT(data_len > 0)
  93. ASSERT(data_len <= enc->buf_size - (enc->buf_start + enc->buf_used))
  94. DebugObject_Access(&enc->d_obj);
  95. // update buffer
  96. enc->buf_used += data_len;
  97. // process data
  98. process_data(enc);
  99. return;
  100. }
  101. void output_handler_done (PacketProtoDecoder *enc)
  102. {
  103. DebugObject_Access(&enc->d_obj);
  104. // process data
  105. process_data(enc);
  106. return;
  107. }
  108. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg, void *user, PacketProtoDecoder_handler_error handler_error)
  109. {
  110. // init arguments
  111. enc->input = input;
  112. enc->output = output;
  113. enc->user = user;
  114. enc->handler_error = handler_error;
  115. // init input
  116. StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);
  117. // init output
  118. PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
  119. // set output MTU, limit by maximum payload size
  120. enc->output_mtu = bmin_int(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
  121. // init buffer state
  122. enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);
  123. enc->buf_start = 0;
  124. enc->buf_used = 0;
  125. // allocate buffer
  126. if (!(enc->buf = (uint8_t *)malloc(enc->buf_size))) {
  127. goto fail0;
  128. }
  129. // start receiving
  130. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf, enc->buf_size);
  131. DebugObject_Init(&enc->d_obj);
  132. return 1;
  133. fail0:
  134. return 0;
  135. }
  136. void PacketProtoDecoder_Free (PacketProtoDecoder *enc)
  137. {
  138. DebugObject_Free(&enc->d_obj);
  139. // free buffer
  140. free(enc->buf);
  141. }
  142. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc)
  143. {
  144. DebugObject_Access(&enc->d_obj);
  145. enc->buf_start += enc->buf_used;
  146. enc->buf_used = 0;
  147. }