FragmentProtoDisassembler.c 6.0 KB

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