PacketProtoDecoder.c 8.1 KB

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