FragmentProtoDisassembler.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @file FragmentProtoDisassembler.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 <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <misc/debug.h>
  26. #include <misc/byteorder.h>
  27. #include <flow/FragmentProtoDisassembler.h>
  28. static void write_chunks (FragmentProtoDisassembler *o)
  29. {
  30. ASSERT(o->in_len >= 0)
  31. ASSERT(o->out)
  32. ASSERT(o->output_mtu - o->out_used >= sizeof(struct fragmentproto_chunk_header))
  33. int in_avail = o->in_len - o->in_used;
  34. int out_avail = (o->output_mtu - o->out_used) - sizeof(struct fragmentproto_chunk_header);
  35. // write chunks to output packet
  36. do {
  37. ASSERT(in_avail >= 0)
  38. ASSERT(!(in_avail == 0) || out_avail >= 0)
  39. // check if we have space in the output packet
  40. // (if this is a zero input packet, only one chunk is written, which
  41. // is always possible)
  42. if (in_avail > 0 && out_avail <= 0) {
  43. break;
  44. }
  45. // calculate chunk length
  46. int chunk_len = in_avail;
  47. if (chunk_len > out_avail) {
  48. chunk_len = out_avail;
  49. }
  50. if (o->chunk_mtu > 0) {
  51. if (chunk_len > o->chunk_mtu) {
  52. chunk_len = o->chunk_mtu;
  53. }
  54. }
  55. // write chunk header
  56. struct fragmentproto_chunk_header *header = (struct fragmentproto_chunk_header *)(o->out + o->out_used);
  57. header->frame_id = htol16(o->frame_id);
  58. header->chunk_start = htol16(o->in_used);
  59. header->chunk_len = htol16(chunk_len);
  60. header->is_last = (chunk_len == in_avail);
  61. // write chunk data
  62. memcpy(o->out + o->out_used + sizeof(struct fragmentproto_chunk_header), o->in + o->in_used, chunk_len);
  63. // increment pointers
  64. o->in_used += chunk_len;
  65. o->out_used += sizeof(struct fragmentproto_chunk_header) + chunk_len;
  66. in_avail = o->in_len - o->in_used;
  67. out_avail = (o->output_mtu - o->out_used) - sizeof(struct fragmentproto_chunk_header);
  68. } while (in_avail > 0);
  69. // have we finished the input packet?
  70. if (in_avail == 0) {
  71. o->in_len = -1;
  72. o->frame_id++;
  73. }
  74. // should we finish the output packet?
  75. if (
  76. out_avail < 0 ||
  77. (in_avail > 0 && out_avail <= 0) ||
  78. o->latency < 0
  79. ) {
  80. // finish output packet
  81. o->out = NULL;
  82. // stop timer (if it's running)
  83. if (o->latency >= 0) {
  84. BReactor_RemoveTimer(o->reactor, &o->timer);
  85. }
  86. } else {
  87. // start timer if we have output and it's not running (output was empty before)
  88. if (!BTimer_IsRunning(&o->timer)) {
  89. BReactor_SetTimer(o->reactor, &o->timer);
  90. }
  91. }
  92. ASSERT(o->in_len < 0 || !o->out)
  93. }
  94. static int input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len)
  95. {
  96. ASSERT(o->in_len == -1)
  97. ASSERT(!o->doing_send)
  98. ASSERT(data_len >= 0)
  99. ASSERT(data_len <= o->input_mtu)
  100. // set input packet
  101. o->in_len = data_len;
  102. o->in = data;
  103. o->in_used = 0;
  104. // if there is no output, block input
  105. if (!o->out) {
  106. return 0;
  107. }
  108. // write input to output
  109. write_chunks(o);
  110. // if we finished the output packet and are not in recv, notify output
  111. if (!o->out && !o->doing_recv) {
  112. o->doing_send = 1;
  113. DEAD_ENTER(o->dead)
  114. PacketRecvInterface_Done(&o->output, o->out_used);
  115. if (DEAD_LEAVE(o->dead)) {
  116. return -1;
  117. }
  118. o->doing_send = 0;
  119. }
  120. // if we still have some input, block input
  121. if (o->in_len >= 0) {
  122. return 0;
  123. }
  124. // all input was processed, accept packet
  125. return 1;
  126. }
  127. static void input_handler_cancel (FragmentProtoDisassembler *o)
  128. {
  129. ASSERT(o->in_len >= 0)
  130. ASSERT(!o->out)
  131. ASSERT(!o->doing_send)
  132. o->in_len = -1;
  133. }
  134. static int output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data, int *data_len)
  135. {
  136. ASSERT(!o->out)
  137. ASSERT(!o->doing_recv)
  138. ASSERT(data)
  139. // set output packet
  140. o->out = data;
  141. o->out_used = 0;
  142. // if there is no input, block output
  143. if (o->in_len < 0) {
  144. return 0;
  145. }
  146. // write input to output
  147. write_chunks(o);
  148. // if we finished the input packet and are not in send, notify input
  149. if (o->in_len < 0 && !o->doing_send) {
  150. o->doing_recv = 1;
  151. DEAD_ENTER(o->dead)
  152. PacketPassInterface_Done(&o->input);
  153. if (DEAD_LEAVE(o->dead)) {
  154. return -1;
  155. }
  156. o->doing_recv = 0;
  157. }
  158. // if we are not going to finish the output packet now, block output
  159. if (o->out) {
  160. return 0;
  161. }
  162. // return packet now
  163. *data_len = o->out_used;
  164. return 1;
  165. }
  166. static void timer_handler (FragmentProtoDisassembler *o)
  167. {
  168. ASSERT(o->latency >= 0)
  169. ASSERT(o->out)
  170. ASSERT(o->in_len = -1)
  171. ASSERT(!o->doing_send)
  172. ASSERT(!o->doing_recv)
  173. // finish output packet
  174. o->out = NULL;
  175. // inform output
  176. PacketRecvInterface_Done(&o->output, o->out_used);
  177. return;
  178. }
  179. void FragmentProtoDisassembler_Init (FragmentProtoDisassembler *o, BReactor *reactor, int input_mtu, int output_mtu, int chunk_mtu, btime_t latency)
  180. {
  181. ASSERT(input_mtu >= 0)
  182. ASSERT(input_mtu <= UINT16_MAX)
  183. ASSERT(output_mtu > sizeof(struct fragmentproto_chunk_header))
  184. ASSERT(chunk_mtu > 0 || chunk_mtu < 0)
  185. // init arguments
  186. o->reactor = reactor;
  187. o->input_mtu = input_mtu;
  188. o->output_mtu = output_mtu;
  189. o->chunk_mtu = chunk_mtu;
  190. o->latency = latency;
  191. // init dead var
  192. DEAD_INIT(o->dead);
  193. // init input
  194. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o);
  195. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  196. // init output
  197. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o);
  198. // init timer
  199. if (o->latency >= 0) {
  200. BTimer_Init(&o->timer, o->latency, (BTimer_handler)timer_handler, o);
  201. }
  202. // have no input packet
  203. o->in_len = -1;
  204. // have no output packet
  205. o->out = NULL;
  206. // start with zero frame ID
  207. o->frame_id = 0;
  208. // not callback from send
  209. o->doing_send = 0;
  210. // not callback from recv
  211. o->doing_recv = 0;
  212. // init debug object
  213. DebugObject_Init(&o->d_obj);
  214. }
  215. void FragmentProtoDisassembler_Free (FragmentProtoDisassembler *o)
  216. {
  217. // free debug object
  218. DebugObject_Free(&o->d_obj);
  219. // free timer
  220. if (o->latency >= 0) {
  221. BReactor_RemoveTimer(o->reactor, &o->timer);
  222. }
  223. // free output
  224. PacketRecvInterface_Free(&o->output);
  225. // free input
  226. PacketPassInterface_Free(&o->input);
  227. // free dead var
  228. DEAD_KILL(o->dead);
  229. }
  230. PacketPassInterface * FragmentProtoDisassembler_GetInput (FragmentProtoDisassembler *o)
  231. {
  232. return &o->input;
  233. }
  234. PacketRecvInterface * FragmentProtoDisassembler_GetOutput (FragmentProtoDisassembler *o)
  235. {
  236. return &o->output;
  237. }