FragmentProtoDisassembler.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // should we finish the output packet?
  55. if (OUT_AVAIL <= 0) {
  56. // set no output packet
  57. o->out = NULL;
  58. // stop timer (if it's running)
  59. BPending_Unset(&o->finish_job);
  60. // finish output
  61. PacketRecvInterface_Done(&o->output, o->out_used);
  62. } else {
  63. // start finish job if we have more space in the output packet. This allows us to
  64. // write more data into this output packet if the input has more data, before we
  65. // submit it out in the job handler.
  66. BPending_Set(&o->finish_job);
  67. }
  68. // have we finished the input packet?
  69. if (IN_AVAIL == 0) {
  70. // set no input packet
  71. o->in_len = -1;
  72. // increment frame ID
  73. o->frame_id++;
  74. // finish input
  75. PacketPassInterface_Done(&o->input);
  76. }
  77. }
  78. static void input_handler_send (FragmentProtoDisassembler *o, uint8_t *data, int data_len)
  79. {
  80. ASSERT(data_len >= 0)
  81. ASSERT(o->in_len == -1)
  82. // set input packet
  83. o->in_len = data_len;
  84. o->in = data;
  85. o->in_used = 0;
  86. // if there is no output, wait for it
  87. if (!o->out) {
  88. return;
  89. }
  90. write_chunks(o);
  91. }
  92. static void input_handler_requestcancel (FragmentProtoDisassembler *o)
  93. {
  94. ASSERT(o->in_len >= 0)
  95. ASSERT(!o->out)
  96. // set no input packet
  97. o->in_len = -1;
  98. // finish input
  99. PacketPassInterface_Done(&o->input);
  100. }
  101. static void output_handler_recv (FragmentProtoDisassembler *o, uint8_t *data)
  102. {
  103. ASSERT(data)
  104. ASSERT(!o->out)
  105. // set output packet
  106. o->out = data;
  107. o->out_used = 0;
  108. // if there is no input, wait for it
  109. if (o->in_len < 0) {
  110. return;
  111. }
  112. write_chunks(o);
  113. }
  114. static void finish_job_handler (FragmentProtoDisassembler *o)
  115. {
  116. ASSERT(o->out)
  117. ASSERT(o->in_len == -1)
  118. // set no output packet
  119. o->out = NULL;
  120. // finish output
  121. PacketRecvInterface_Done(&o->output, o->out_used);
  122. }
  123. void FragmentProtoDisassembler_Init (FragmentProtoDisassembler *o, BPendingGroup *pg, int input_mtu, int output_mtu, int chunk_mtu)
  124. {
  125. ASSERT(input_mtu >= 0)
  126. ASSERT(input_mtu <= UINT16_MAX)
  127. ASSERT(output_mtu > sizeof(struct fragmentproto_chunk_header))
  128. ASSERT(chunk_mtu > 0 || chunk_mtu < 0)
  129. // init arguments
  130. o->output_mtu = output_mtu;
  131. o->chunk_mtu = chunk_mtu;
  132. // init input
  133. PacketPassInterface_Init(&o->input, input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  134. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  135. // init output
  136. PacketRecvInterface_Init(&o->output, o->output_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  137. // init finish job
  138. BPending_Init(&o->finish_job, pg, (BPending_handler)finish_job_handler, o);
  139. // have no input packet
  140. o->in_len = -1;
  141. // have no output packet
  142. o->out = NULL;
  143. // start with zero frame ID
  144. o->frame_id = 0;
  145. DebugObject_Init(&o->d_obj);
  146. }
  147. void FragmentProtoDisassembler_Free (FragmentProtoDisassembler *o)
  148. {
  149. DebugObject_Free(&o->d_obj);
  150. // free finish job
  151. BPending_Free(&o->finish_job);
  152. // free output
  153. PacketRecvInterface_Free(&o->output);
  154. // free input
  155. PacketPassInterface_Free(&o->input);
  156. }
  157. PacketPassInterface * FragmentProtoDisassembler_GetInput (FragmentProtoDisassembler *o)
  158. {
  159. DebugObject_Access(&o->d_obj);
  160. return &o->input;
  161. }
  162. PacketRecvInterface * FragmentProtoDisassembler_GetOutput (FragmentProtoDisassembler *o)
  163. {
  164. DebugObject_Access(&o->d_obj);
  165. return &o->output;
  166. }