PacketProtoDecoder.c 4.9 KB

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