PacketProtoDecoder.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 int call_recv (PacketProtoDecoder *enc, uint8_t *data, int avail);
  30. static int call_send (PacketProtoDecoder *enc, uint8_t *data, int len);
  31. static void receive_data (PacketProtoDecoder *enc);
  32. static void input_handler_done (PacketProtoDecoder *enc, int data_len);
  33. static int parse_and_send (PacketProtoDecoder *enc);
  34. static void output_handler_done (PacketProtoDecoder *enc);
  35. static void job_handler (PacketProtoDecoder *enc);
  36. void report_error (PacketProtoDecoder *enc, int error)
  37. {
  38. #ifndef NDEBUG
  39. DEAD_ENTER(enc->dead)
  40. #endif
  41. FlowErrorReporter_ReportError(&enc->rep, &error);
  42. #ifndef NDEBUG
  43. ASSERT(DEAD_KILLED)
  44. DEAD_LEAVE(enc->dead);
  45. #endif
  46. }
  47. int call_recv (PacketProtoDecoder *enc, uint8_t *data, int avail)
  48. {
  49. ASSERT(avail > 0)
  50. ASSERT(!StreamRecvInterface_InClient(enc->input))
  51. DEAD_ENTER(enc->dead)
  52. int res = StreamRecvInterface_Receiver_Recv(enc->input, data, avail);
  53. if (DEAD_LEAVE(enc->dead)) {
  54. return -1;
  55. }
  56. ASSERT(res >= 0)
  57. ASSERT(res <= avail)
  58. return res;
  59. }
  60. int call_send (PacketProtoDecoder *enc, uint8_t *data, int len)
  61. {
  62. ASSERT(len >= 0)
  63. ASSERT(len <= enc->output_mtu)
  64. ASSERT(!PacketPassInterface_InClient(enc->output))
  65. DEAD_ENTER(enc->dead)
  66. int res = PacketPassInterface_Sender_Send(enc->output, data, len);
  67. if (DEAD_LEAVE(enc->dead)) {
  68. return -1;
  69. }
  70. ASSERT(res == 0 || res == 1)
  71. return res;
  72. }
  73. void receive_data (PacketProtoDecoder *enc)
  74. {
  75. ASSERT(!enc->receiving)
  76. ASSERT(!enc->sending)
  77. ASSERT(enc->buf_start + enc->buf_used < enc->buf_size)
  78. ASSERT(!StreamRecvInterface_InClient(enc->input))
  79. ASSERT(!PacketPassInterface_InClient(enc->output))
  80. do {
  81. // receive data
  82. int res;
  83. if ((res = call_recv(
  84. enc,
  85. enc->buf + (enc->buf_start + enc->buf_used),
  86. enc->buf_size - (enc->buf_start + enc->buf_used)
  87. )) < 0) {
  88. return;
  89. }
  90. if (res == 0) {
  91. // input busy, continue in input_handler_done
  92. enc->receiving = 1;
  93. break;
  94. }
  95. // update buffer
  96. enc->buf_used += res;
  97. // parse and send data
  98. if (parse_and_send(enc) < 0) {
  99. return;
  100. }
  101. } while (!enc->sending && enc->buf_start + enc->buf_used < enc->buf_size);
  102. }
  103. static void input_handler_done (PacketProtoDecoder *enc, int data_len)
  104. {
  105. ASSERT(enc->receiving)
  106. ASSERT(!enc->sending)
  107. ASSERT(enc->buf_start + enc->buf_used < enc->buf_size)
  108. ASSERT(data_len > 0)
  109. ASSERT(data_len <= enc->buf_size - (enc->buf_start + enc->buf_used))
  110. ASSERT(!StreamRecvInterface_InClient(enc->input))
  111. ASSERT(!PacketPassInterface_InClient(enc->output))
  112. // set not receiving
  113. enc->receiving = 0;
  114. // update buffer
  115. enc->buf_used += data_len;
  116. // parse and send data
  117. if (parse_and_send(enc) < 0) {
  118. return;
  119. }
  120. // continue receiving
  121. if (!enc->sending && enc->buf_start + enc->buf_used < enc->buf_size) {
  122. receive_data(enc);
  123. return;
  124. }
  125. }
  126. int parse_and_send (PacketProtoDecoder *enc)
  127. {
  128. ASSERT(!enc->sending)
  129. ASSERT(!StreamRecvInterface_InClient(enc->input))
  130. ASSERT(!PacketPassInterface_InClient(enc->output))
  131. while (1) {
  132. uint8_t *data = enc->buf + enc->buf_start;
  133. int left = enc->buf_used;
  134. // check if header was received
  135. if (left < sizeof(struct packetproto_header)) {
  136. break;
  137. }
  138. struct packetproto_header *header = (struct packetproto_header *)data;
  139. data += sizeof(struct packetproto_header);
  140. left -= sizeof(struct packetproto_header);
  141. int data_len = ltoh16(header->len);
  142. // check data length
  143. if (data_len > enc->output_mtu) {
  144. report_error(enc, PACKETPROTODECODER_ERROR_TOOLONG);
  145. return -1;
  146. }
  147. // check if whole packet was received
  148. if (left < data_len) {
  149. break;
  150. }
  151. // update buffer
  152. enc->buf_start += sizeof(struct packetproto_header) + data_len;
  153. enc->buf_used -= sizeof(struct packetproto_header) + data_len;
  154. // submit packet
  155. int res;
  156. if ((res = call_send(enc, data, data_len)) < 0) {
  157. return -1;
  158. }
  159. if (!res) {
  160. // output busy, continue in output_handler_done
  161. enc->sending = 1;
  162. return 0;
  163. }
  164. }
  165. // if we reached the end of the buffer, wrap around to allow more data to be received
  166. if (enc->buf_start + enc->buf_used == enc->buf_size) {
  167. memmove(enc->buf, enc->buf + enc->buf_start, enc->buf_used);
  168. enc->buf_start = 0;
  169. }
  170. return 0;
  171. }
  172. void output_handler_done (PacketProtoDecoder *enc)
  173. {
  174. ASSERT(enc->sending)
  175. ASSERT(!StreamRecvInterface_InClient(enc->input))
  176. ASSERT(!PacketPassInterface_InClient(enc->output))
  177. // set not sending
  178. enc->sending = 0;
  179. // continue parsing and sending
  180. if (parse_and_send(enc) < 0) {
  181. return;
  182. }
  183. // continue receiving
  184. if (!enc->receiving && !enc->sending && enc->buf_start + enc->buf_used < enc->buf_size) {
  185. receive_data(enc);
  186. return;
  187. }
  188. }
  189. void job_handler (PacketProtoDecoder *enc)
  190. {
  191. receive_data(enc);
  192. return;
  193. }
  194. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg)
  195. {
  196. // init arguments
  197. enc->rep = rep;
  198. enc->input = input;
  199. enc->output = output;
  200. // init dead var
  201. DEAD_INIT(enc->dead);
  202. // init input
  203. StreamRecvInterface_Receiver_Init(enc->input, (StreamRecvInterface_handler_done)input_handler_done, enc);
  204. // init output
  205. PacketPassInterface_Sender_Init(enc->output, (PacketPassInterface_handler_done)output_handler_done, enc);
  206. // set output MTU, limit by maximum payload size
  207. enc->output_mtu = BMIN(PacketPassInterface_GetMTU(enc->output), PACKETPROTO_MAXPAYLOAD);
  208. // init buffer state
  209. enc->buf_size = PACKETPROTO_ENCLEN(enc->output_mtu);
  210. enc->buf_start = 0;
  211. enc->buf_used = 0;
  212. // allocate buffer
  213. if (!(enc->buf = malloc(enc->buf_size))) {
  214. goto fail0;
  215. }
  216. // set not receiving
  217. enc->receiving = 0;
  218. // set not sending
  219. enc->sending = 0;
  220. // init start job
  221. BPending_Init(&enc->start_job, pg, (BPending_handler)job_handler, enc);
  222. BPending_Set(&enc->start_job);
  223. // init debug object
  224. DebugObject_Init(&enc->d_obj);
  225. return 1;
  226. fail0:
  227. return 0;
  228. }
  229. void PacketProtoDecoder_Free (PacketProtoDecoder *enc)
  230. {
  231. // free debug object
  232. DebugObject_Free(&enc->d_obj);
  233. // free start job
  234. BPending_Free(&enc->start_job);
  235. // free buffer
  236. free(enc->buf);
  237. // free dead var
  238. DEAD_KILL(enc->dead);
  239. }
  240. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc)
  241. {
  242. enc->buf_start += enc->buf_used;
  243. enc->buf_used = 0;
  244. }