FragmentProtoDisassembler.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len)
  95. {
  96. ASSERT(o->in_len == -1)
  97. ASSERT(data_len >= 0)
  98. ASSERT(data_len <= o->input_mtu)
  99. // set input packet
  100. o->in_len = data_len;
  101. o->in = data;
  102. o->in_used = 0;
  103. // if there is no output, wait for it
  104. if (!o->out) {
  105. return;
  106. }
  107. // write input to output
  108. write_chunks(o);
  109. // finish input packet if needed
  110. if (o->in_len == -1) {
  111. PacketPassInterface_Done(&o->input);
  112. }
  113. // finish output packet if needed
  114. if (!o->out) {
  115. PacketRecvInterface_Done(&o->output, o->out_used);
  116. }
  117. }
  118. static void input_handler_cancel (FragmentProtoDisassembler *o)
  119. {
  120. ASSERT(o->in_len >= 0)
  121. ASSERT(!o->out)
  122. o->in_len = -1;
  123. }
  124. static void output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data)
  125. {
  126. ASSERT(!o->out)
  127. ASSERT(data)
  128. // set output packet
  129. o->out = data;
  130. o->out_used = 0;
  131. // if there is no input, wait for it
  132. if (o->in_len < 0) {
  133. return;
  134. }
  135. // write input to output
  136. write_chunks(o);
  137. // finish output packet if needed
  138. if (!o->out) {
  139. PacketRecvInterface_Done(&o->output, o->out_used);
  140. }
  141. // finish input packet if needed
  142. if (o->in_len == -1) {
  143. PacketPassInterface_Done(&o->input);
  144. }
  145. }
  146. static void timer_handler (FragmentProtoDisassembler *o)
  147. {
  148. ASSERT(o->latency >= 0)
  149. ASSERT(o->out)
  150. ASSERT(o->in_len = -1)
  151. // finish output packet
  152. o->out = NULL;
  153. PacketRecvInterface_Done(&o->output, o->out_used);
  154. }
  155. void FragmentProtoDisassembler_Init (FragmentProtoDisassembler *o, BReactor *reactor, int input_mtu, int output_mtu, int chunk_mtu, btime_t latency)
  156. {
  157. ASSERT(input_mtu >= 0)
  158. ASSERT(input_mtu <= UINT16_MAX)
  159. ASSERT(output_mtu > sizeof(struct fragmentproto_chunk_header))
  160. ASSERT(chunk_mtu > 0 || chunk_mtu < 0)
  161. // init arguments
  162. o->reactor = reactor;
  163. o->input_mtu = input_mtu;
  164. o->output_mtu = output_mtu;
  165. o->chunk_mtu = chunk_mtu;
  166. o->latency = latency;
  167. // init input
  168. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(reactor));
  169. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  170. // init output
  171. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, BReactor_PendingGroup(reactor));
  172. // init timer
  173. if (o->latency >= 0) {
  174. BTimer_Init(&o->timer, o->latency, (BTimer_handler)timer_handler, o);
  175. }
  176. // have no input packet
  177. o->in_len = -1;
  178. // have no output packet
  179. o->out = NULL;
  180. // start with zero frame ID
  181. o->frame_id = 0;
  182. DebugObject_Init(&o->d_obj);
  183. }
  184. void FragmentProtoDisassembler_Free (FragmentProtoDisassembler *o)
  185. {
  186. DebugObject_Free(&o->d_obj);
  187. // free timer
  188. if (o->latency >= 0) {
  189. BReactor_RemoveTimer(o->reactor, &o->timer);
  190. }
  191. // free output
  192. PacketRecvInterface_Free(&o->output);
  193. // free input
  194. PacketPassInterface_Free(&o->input);
  195. }
  196. PacketPassInterface * FragmentProtoDisassembler_GetInput (FragmentProtoDisassembler *o)
  197. {
  198. DebugObject_Access(&o->d_obj);
  199. return &o->input;
  200. }
  201. PacketRecvInterface * FragmentProtoDisassembler_GetOutput (FragmentProtoDisassembler *o)
  202. {
  203. DebugObject_Access(&o->d_obj);
  204. return &o->output;
  205. }