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. #ifndef NDEBUG
  35. DEAD_ENTER(enc->d_dead)
  36. #endif
  37. FlowErrorReporter_ReportError(&enc->rep, &error);
  38. #ifndef NDEBUG
  39. ASSERT(DEAD_KILLED)
  40. DEAD_LEAVE(enc->d_dead);
  41. #endif
  42. }
  43. void process_data (PacketProtoDecoder *enc)
  44. {
  45. do {
  46. uint8_t *data = enc->buf + enc->buf_start;
  47. int left = enc->buf_used;
  48. // check if header was received
  49. if (left < sizeof(struct packetproto_header)) {
  50. break;
  51. }
  52. struct packetproto_header *header = (struct packetproto_header *)data;
  53. data += sizeof(struct packetproto_header);
  54. left -= sizeof(struct packetproto_header);
  55. int data_len = ltoh16(header->len);
  56. // check data length
  57. if (data_len > enc->output_mtu) {
  58. report_error(enc, PACKETPROTODECODER_ERROR_TOOLONG);
  59. return;
  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 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. // receive data
  78. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf + (enc->buf_start + enc->buf_used), enc->buf_size - ((enc->buf_start + enc->buf_used)));
  79. }
  80. static void input_handler_done (PacketProtoDecoder *enc, int data_len)
  81. {
  82. ASSERT(data_len > 0)
  83. ASSERT(data_len <= enc->buf_size - (enc->buf_start + enc->buf_used))
  84. DebugObject_Access(&enc->d_obj);
  85. // update buffer
  86. enc->buf_used += data_len;
  87. // process data
  88. process_data(enc);
  89. return;
  90. }
  91. void output_handler_done (PacketProtoDecoder *enc)
  92. {
  93. DebugObject_Access(&enc->d_obj);
  94. // process data
  95. process_data(enc);
  96. return;
  97. }
  98. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg)
  99. {
  100. // init arguments
  101. enc->rep = rep;
  102. enc->input = input;
  103. enc->output = output;
  104. // init input
  105. StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);
  106. // init output
  107. PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
  108. // set output MTU, limit by maximum payload size
  109. enc->output_mtu = BMIN(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
  110. // init buffer state
  111. enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);
  112. enc->buf_start = 0;
  113. enc->buf_used = 0;
  114. // allocate buffer
  115. if (!(enc->buf = malloc(enc->buf_size))) {
  116. goto fail0;
  117. }
  118. // start receiving
  119. StreamRecvInterface_Receiver_Recv(enc->input, enc->buf, enc->buf_size);
  120. DebugObject_Init(&enc->d_obj);
  121. #ifndef NDEBUG
  122. DEAD_INIT(enc->d_dead);
  123. #endif
  124. return 1;
  125. fail0:
  126. return 0;
  127. }
  128. void PacketProtoDecoder_Free (PacketProtoDecoder *enc)
  129. {
  130. DebugObject_Free(&enc->d_obj);
  131. #ifndef NDEBUG
  132. DEAD_KILL(enc->d_dead);
  133. #endif
  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. }