FragmentProtoDisassembler.c 5.9 KB

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