PacketProtoDecoder.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file PacketProtoDecoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <misc/debug.h>
  25. #include <misc/byteorder.h>
  26. #include <misc/minmax.h>
  27. #include <flow/PacketProtoDecoder.h>
  28. static void report_error (PacketProtoDecoder *enc, int error);
  29. static void process_data (PacketProtoDecoder *enc);
  30. static void input_handler_done (PacketProtoDecoder *enc, int data_len);
  31. static void output_handler_done (PacketProtoDecoder *enc);
  32. void report_error (PacketProtoDecoder *enc, int error)
  33. {
  34. FlowErrorReporter_ReportError(&enc->rep, error);
  35. return;
  36. }
  37. void process_data (PacketProtoDecoder *enc)
  38. {
  39. int was_error = 0;
  40. do {
  41. uint8_t *data = enc->buf + enc->buf_start;
  42. int left = enc->buf_used;
  43. // check if header was received
  44. if (left < sizeof(struct packetproto_header)) {
  45. break;
  46. }
  47. struct packetproto_header *header = (struct packetproto_header *)data;
  48. data += sizeof(struct packetproto_header);
  49. left -= sizeof(struct packetproto_header);
  50. int data_len = ltoh16(header->len);
  51. // check data length
  52. if (data_len > enc->output_mtu) {
  53. was_error = 1;
  54. break;
  55. }
  56. // check if whole packet was received
  57. if (left < data_len) {
  58. break;
  59. }
  60. // update buffer
  61. enc->buf_start += sizeof(struct packetproto_header) + data_len;
  62. enc->buf_used -= sizeof(struct packetproto_header) + data_len;
  63. // submit packet
  64. PacketPassInterface_Sender_Send(enc->output, data, data_len);
  65. return;
  66. } while (0);
  67. if (was_error) {
  68. // reset buffer
  69. enc->buf_start = 0;
  70. enc->buf_used = 0;
  71. } else {
  72. // if we reached the end of the buffer, wrap around to allow more data to be received
  73. if (enc->buf_start + enc->buf_used == enc->buf_size) {
  74. memmove(enc->buf, enc->buf + enc->buf_start, enc->buf_used);
  75. enc->buf_start = 0;
  76. }
  77. }
  78. // receive data
  79. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf + (enc->buf_start + enc->buf_used), enc->buf_size - (enc->buf_start + enc->buf_used));
  80. // if we had error, report it
  81. if (was_error) {
  82. report_error(enc, PACKETPROTODECODER_ERROR_TOOLONG);
  83. return;
  84. }
  85. }
  86. static void input_handler_done (PacketProtoDecoder *enc, int data_len)
  87. {
  88. ASSERT(data_len > 0)
  89. ASSERT(data_len <= enc->buf_size - (enc->buf_start + enc->buf_used))
  90. DebugObject_Access(&enc->d_obj);
  91. // update buffer
  92. enc->buf_used += data_len;
  93. // process data
  94. process_data(enc);
  95. return;
  96. }
  97. void output_handler_done (PacketProtoDecoder *enc)
  98. {
  99. DebugObject_Access(&enc->d_obj);
  100. // process data
  101. process_data(enc);
  102. return;
  103. }
  104. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg)
  105. {
  106. // init arguments
  107. enc->rep = rep;
  108. enc->input = input;
  109. enc->output = output;
  110. // init input
  111. StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);
  112. // init output
  113. PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
  114. // set output MTU, limit by maximum payload size
  115. enc->output_mtu = BMIN(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
  116. // init buffer state
  117. enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);
  118. enc->buf_start = 0;
  119. enc->buf_used = 0;
  120. // allocate buffer
  121. if (!(enc->buf = malloc(enc->buf_size))) {
  122. goto fail0;
  123. }
  124. // start receiving
  125. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf, enc->buf_size);
  126. DebugObject_Init(&enc->d_obj);
  127. return 1;
  128. fail0:
  129. return 0;
  130. }
  131. void PacketProtoDecoder_Free (PacketProtoDecoder *enc)
  132. {
  133. DebugObject_Free(&enc->d_obj);
  134. // free buffer
  135. free(enc->buf);
  136. }
  137. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc)
  138. {
  139. DebugObject_Access(&enc->d_obj);
  140. enc->buf_start += enc->buf_used;
  141. enc->buf_used = 0;
  142. }