FragmentProtoDisassembler.c 6.8 KB

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