FragmentProtoDisassembler.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <stddef.h>
  24. #include <string.h>
  25. #include <misc/debug.h>
  26. #include <misc/byteorder.h>
  27. #include <misc/minmax.h>
  28. #include <flow/FragmentProtoDisassembler.h>
  29. static void write_chunks (FragmentProtoDisassembler *o)
  30. {
  31. #define IN_AVAIL (o->in_len - o->in_used)
  32. #define OUT_AVAIL ((o->output_mtu - o->out_used) - (int)sizeof(struct fragmentproto_chunk_header))
  33. ASSERT(o->in_len >= 0)
  34. ASSERT(o->out)
  35. ASSERT(OUT_AVAIL > 0)
  36. // write chunks to output packet
  37. do {
  38. // calculate chunk length
  39. int chunk_len = bmin_int(IN_AVAIL, OUT_AVAIL);
  40. if (o->chunk_mtu > 0) {
  41. chunk_len = bmin_int(chunk_len, o->chunk_mtu);
  42. }
  43. // write chunk header
  44. struct fragmentproto_chunk_header *header = (struct fragmentproto_chunk_header *)(o->out + o->out_used);
  45. header->frame_id = htol16(o->frame_id);
  46. header->chunk_start = htol16(o->in_used);
  47. header->chunk_len = htol16(chunk_len);
  48. header->is_last = (chunk_len == IN_AVAIL);
  49. // write chunk data
  50. memcpy(o->out + o->out_used + sizeof(struct fragmentproto_chunk_header), o->in + o->in_used, chunk_len);
  51. // increment pointers
  52. o->in_used += chunk_len;
  53. o->out_used += sizeof(struct fragmentproto_chunk_header) + chunk_len;
  54. } while (IN_AVAIL > 0 && OUT_AVAIL > 0);
  55. // have we finished the input packet?
  56. if (IN_AVAIL == 0) {
  57. // set no input packet
  58. o->in_len = -1;
  59. // increment frame ID
  60. o->frame_id++;
  61. // finish input
  62. PacketPassInterface_Done(&o->input);
  63. }
  64. // should we finish the output packet?
  65. if (OUT_AVAIL <= 0 || o->latency < 0) {
  66. // set no output packet
  67. o->out = NULL;
  68. // stop timer (if it's running)
  69. if (o->latency >= 0) {
  70. BReactor_RemoveTimer(o->reactor, &o->timer);
  71. }
  72. // finish output
  73. PacketRecvInterface_Done(&o->output, o->out_used);
  74. } else {
  75. // start timer if we have output and it's not running (output was empty before)
  76. if (!BTimer_IsRunning(&o->timer)) {
  77. BReactor_SetTimer(o->reactor, &o->timer);
  78. }
  79. }
  80. }
  81. static void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len)
  82. {
  83. ASSERT(data_len >= 0)
  84. ASSERT(o->in_len == -1)
  85. // set input packet
  86. o->in_len = data_len;
  87. o->in = data;
  88. o->in_used = 0;
  89. // if there is no output, wait for it
  90. if (!o->out) {
  91. return;
  92. }
  93. write_chunks(o);
  94. }
  95. static void input_handler_requestcancel (FragmentProtoDisassembler *o)
  96. {
  97. ASSERT(o->in_len >= 0)
  98. ASSERT(!o->out)
  99. // set no input packet
  100. o->in_len = -1;
  101. // finish input
  102. PacketPassInterface_Done(&o->input);
  103. }
  104. static void output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data)
  105. {
  106. ASSERT(data)
  107. ASSERT(!o->out)
  108. // set output packet
  109. o->out = data;
  110. o->out_used = 0;
  111. // if there is no input, wait for it
  112. if (o->in_len < 0) {
  113. return;
  114. }
  115. write_chunks(o);
  116. }
  117. static void timer_handler (FragmentProtoDisassembler *o)
  118. {
  119. ASSERT(o->latency >= 0)
  120. ASSERT(o->out)
  121. ASSERT(o->in_len == -1)
  122. // set no output packet
  123. o->out = NULL;
  124. // finish output
  125. PacketRecvInterface_Done(&o->output, o->out_used);
  126. }
  127. void FragmentProtoDisassembler_Init (FragmentProtoDisassembler *o, BReactor *reactor, int input_mtu, int output_mtu, int chunk_mtu, btime_t latency)
  128. {
  129. ASSERT(input_mtu >= 0)
  130. ASSERT(input_mtu <= UINT16_MAX)
  131. ASSERT(output_mtu > sizeof(struct fragmentproto_chunk_header))
  132. ASSERT(chunk_mtu > 0 || chunk_mtu < 0)
  133. // init arguments
  134. o->reactor = reactor;
  135. o->output_mtu = output_mtu;
  136. o->chunk_mtu = chunk_mtu;
  137. o->latency = latency;
  138. // init input
  139. PacketPassInterface_Init(&o->input, input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(reactor));
  140. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  141. // init output
  142. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, BReactor_PendingGroup(reactor));
  143. // init timer
  144. if (o->latency >= 0) {
  145. BTimer_Init(&o->timer, o->latency, (BTimer_handler)timer_handler, o);
  146. }
  147. // have no input packet
  148. o->in_len = -1;
  149. // have no output packet
  150. o->out = NULL;
  151. // start with zero frame ID
  152. o->frame_id = 0;
  153. DebugObject_Init(&o->d_obj);
  154. }
  155. void FragmentProtoDisassembler_Free (FragmentProtoDisassembler *o)
  156. {
  157. DebugObject_Free(&o->d_obj);
  158. // free timer
  159. if (o->latency >= 0) {
  160. BReactor_RemoveTimer(o->reactor, &o->timer);
  161. }
  162. // free output
  163. PacketRecvInterface_Free(&o->output);
  164. // free input
  165. PacketPassInterface_Free(&o->input);
  166. }
  167. PacketPassInterface * FragmentProtoDisassembler_GetInput (FragmentProtoDisassembler *o)
  168. {
  169. DebugObject_Access(&o->d_obj);
  170. return &o->input;
  171. }
  172. PacketRecvInterface * FragmentProtoDisassembler_GetOutput (FragmentProtoDisassembler *o)
  173. {
  174. DebugObject_Access(&o->d_obj);
  175. return &o->output;
  176. }