FragmentProtoAssembler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /**
  2. * @file FragmentProtoAssembler.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 <stdlib.h>
  23. #include <string.h>
  24. #include <misc/offset.h>
  25. #include <misc/byteorder.h>
  26. #include <misc/balloc.h>
  27. #include "FragmentProtoAssembler.h"
  28. #include <generated/blog_channel_FragmentProtoAssembler.h>
  29. #define PeerLog(_o, ...) BLog_LogViaFunc((_o)->logfunc, (_o)->user, BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  30. static int frame_id_comparator (void *unused, fragmentproto_frameid *v1, fragmentproto_frameid *v2)
  31. {
  32. if (*v1 < *v2) {
  33. return -1;
  34. }
  35. if (*v1 > *v2) {
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static void free_frame (FragmentProtoAssembler *o, struct FragmentProtoAssembler_frame *frame)
  41. {
  42. // remove from used list
  43. LinkedList2_Remove(&o->frames_used, &frame->list_node);
  44. // remove from used tree
  45. BAVL_Remove(&o->frames_used_tree, &frame->tree_node);
  46. // append to free list
  47. LinkedList2_Append(&o->frames_free, &frame->list_node);
  48. }
  49. static void free_oldest_frame (FragmentProtoAssembler *o)
  50. {
  51. ASSERT(!LinkedList2_IsEmpty(&o->frames_used))
  52. // obtain oldest frame (first on the list)
  53. LinkedList2Node *list_node = LinkedList2_GetFirst(&o->frames_used);
  54. ASSERT(list_node)
  55. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  56. // free frame
  57. free_frame(o, frame);
  58. }
  59. static struct FragmentProtoAssembler_frame * allocate_new_frame (FragmentProtoAssembler *o, fragmentproto_frameid id)
  60. {
  61. ASSERT(!BAVL_LookupExact(&o->frames_used_tree, &id))
  62. // if there are no free entries, free the oldest used one
  63. if (LinkedList2_IsEmpty(&o->frames_free)) {
  64. PeerLog(o, BLOG_INFO, "freeing used frame");
  65. free_oldest_frame(o);
  66. }
  67. // obtain frame entry
  68. LinkedList2Node *list_node = LinkedList2_GetFirst(&o->frames_free);
  69. ASSERT(list_node)
  70. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  71. // remove from free list
  72. LinkedList2_Remove(&o->frames_free, &frame->list_node);
  73. // initialize values
  74. frame->id = id;
  75. frame->time = o->time;
  76. frame->num_chunks = 0;
  77. frame->sum = 0;
  78. frame->length = -1;
  79. frame->length_so_far = 0;
  80. // append to used list
  81. LinkedList2_Append(&o->frames_used, &frame->list_node);
  82. // insert to used tree
  83. ASSERT_EXECUTE(BAVL_Insert(&o->frames_used_tree, &frame->tree_node, NULL))
  84. return frame;
  85. }
  86. static int chunks_overlap (int c1_start, int c1_len, int c2_start, int c2_len)
  87. {
  88. return (c1_start + c1_len > c2_start && c2_start + c2_len > c1_start);
  89. }
  90. static int frame_is_timed_out (FragmentProtoAssembler *o, struct FragmentProtoAssembler_frame *frame)
  91. {
  92. ASSERT(frame->time <= o->time)
  93. return (o->time - frame->time > o->time_tolerance);
  94. }
  95. static void reduce_times (FragmentProtoAssembler *o)
  96. {
  97. // find the frame with minimal time, removing timed out frames
  98. struct FragmentProtoAssembler_frame *minframe = NULL;
  99. LinkedList2Iterator it;
  100. LinkedList2Iterator_InitForward(&it, &o->frames_used);
  101. LinkedList2Node *list_node;
  102. while (list_node = LinkedList2Iterator_Next(&it)) {
  103. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  104. if (frame_is_timed_out(o, frame)) {
  105. PeerLog(o, BLOG_INFO, "freeing timed out frame (while reducing times)");
  106. free_frame(o, frame);
  107. } else {
  108. if (!minframe || frame->time < minframe->time) {
  109. minframe = frame;
  110. }
  111. }
  112. }
  113. if (!minframe) {
  114. // have no frames, set packet time to zero
  115. o->time = 0;
  116. return;
  117. }
  118. uint32_t min_time = minframe->time;
  119. // subtract minimal time from all frames
  120. LinkedList2Iterator_InitForward(&it, &o->frames_used);
  121. while (list_node = LinkedList2Iterator_Next(&it)) {
  122. struct FragmentProtoAssembler_frame *frame = UPPER_OBJECT(list_node, struct FragmentProtoAssembler_frame, list_node);
  123. frame->time -= min_time;
  124. }
  125. // subtract minimal time from packet time
  126. o->time -= min_time;
  127. }
  128. static int process_chunk (FragmentProtoAssembler *o, fragmentproto_frameid frame_id, int chunk_start, int chunk_len, int is_last, uint8_t *payload)
  129. {
  130. ASSERT(chunk_start >= 0)
  131. ASSERT(chunk_len >= 0)
  132. ASSERT(is_last == 0 || is_last == 1)
  133. // verify chunk
  134. // check start
  135. if (chunk_start > o->output_mtu) {
  136. PeerLog(o, BLOG_INFO, "chunk starts outside");
  137. return 0;
  138. }
  139. // check frame size bound
  140. if (chunk_len > o->output_mtu - chunk_start) {
  141. PeerLog(o, BLOG_INFO, "chunk ends outside");
  142. return 0;
  143. }
  144. // calculate end
  145. int chunk_end = chunk_start + chunk_len;
  146. ASSERT(chunk_end >= 0)
  147. ASSERT(chunk_end <= o->output_mtu)
  148. // lookup frame
  149. struct FragmentProtoAssembler_frame *frame;
  150. BAVLNode *tree_node;
  151. if (!(tree_node = BAVL_LookupExact(&o->frames_used_tree, &frame_id))) {
  152. // frame not found, add a new one
  153. frame = allocate_new_frame(o, frame_id);
  154. } else {
  155. // have existing frame with that ID
  156. frame = UPPER_OBJECT(tree_node, struct FragmentProtoAssembler_frame, tree_node);
  157. // check frame time
  158. if (frame_is_timed_out(o, frame)) {
  159. // frame is timed out, remove it and use a new one
  160. PeerLog(o, BLOG_INFO, "freeing timed out frame (while processing chunk)");
  161. free_frame(o, frame);
  162. frame = allocate_new_frame(o, frame_id);
  163. }
  164. }
  165. ASSERT(frame->num_chunks < o->num_chunks)
  166. // check if the chunk overlaps with any existing chunks
  167. for (int i = 0; i < frame->num_chunks; i++) {
  168. struct FragmentProtoAssembler_chunk *chunk = &frame->chunks[i];
  169. if (chunks_overlap(chunk->start, chunk->len, chunk_start, chunk_len)) {
  170. PeerLog(o, BLOG_INFO, "chunk overlaps with existing chunk");
  171. goto fail_frame;
  172. }
  173. }
  174. if (is_last) {
  175. // this chunk is marked as last
  176. if (frame->length >= 0) {
  177. PeerLog(o, BLOG_INFO, "got last chunk, but already have one");
  178. goto fail_frame;
  179. }
  180. // check if frame size according to this packet is consistent
  181. // with existing chunks
  182. if (frame->length_so_far > chunk_end) {
  183. PeerLog(o, BLOG_INFO, "got last chunk, but already have data over its bound");
  184. goto fail_frame;
  185. }
  186. } else {
  187. // if we have length, chunk must be in its bound
  188. if (frame->length >= 0) {
  189. if (chunk_end > frame->length) {
  190. PeerLog(o, BLOG_INFO, "chunk out of length bound");
  191. goto fail_frame;
  192. }
  193. }
  194. }
  195. // chunk is good, add it
  196. // update frame time
  197. frame->time = o->time;
  198. // add chunk entry
  199. struct FragmentProtoAssembler_chunk *chunk = &frame->chunks[frame->num_chunks];
  200. chunk->start = chunk_start;
  201. chunk->len = chunk_len;
  202. frame->num_chunks++;
  203. // update sum
  204. frame->sum += chunk_len;
  205. // update length
  206. if (is_last) {
  207. frame->length = chunk_end;
  208. } else {
  209. if (frame->length < 0) {
  210. if (frame->length_so_far < chunk_end) {
  211. frame->length_so_far = chunk_end;
  212. }
  213. }
  214. }
  215. // copy chunk payload to buffer
  216. memcpy(frame->buffer + chunk_start, payload, chunk_len);
  217. // is frame incomplete?
  218. if (frame->length < 0 || frame->sum < frame->length) {
  219. // if all chunks are used, fail it
  220. if (frame->num_chunks == o->num_chunks) {
  221. PeerLog(o, BLOG_INFO, "all chunks used, but frame not complete");
  222. goto fail_frame;
  223. }
  224. // wait for more chunks
  225. return 0;
  226. }
  227. ASSERT(frame->sum == frame->length)
  228. PeerLog(o, BLOG_DEBUG, "frame complete");
  229. // free frame entry
  230. free_frame(o, frame);
  231. // send frame
  232. PacketPassInterface_Sender_Send(o->output, frame->buffer, frame->length);
  233. return 1;
  234. fail_frame:
  235. free_frame(o, frame);
  236. return 0;
  237. }
  238. static void process_input (FragmentProtoAssembler *o)
  239. {
  240. ASSERT(o->in_len >= 0)
  241. // read chunks
  242. while (o->in_pos < o->in_len) {
  243. // obtain header
  244. if (o->in_len - o->in_pos < sizeof(struct fragmentproto_chunk_header)) {
  245. PeerLog(o, BLOG_INFO, "too little data for chunk header");
  246. break;
  247. }
  248. struct fragmentproto_chunk_header *header = (struct fragmentproto_chunk_header *)(o->in + o->in_pos);
  249. o->in_pos += sizeof(struct fragmentproto_chunk_header);
  250. fragmentproto_frameid frame_id = ltoh16(header->frame_id);
  251. int chunk_start = ltoh16(header->chunk_start);
  252. int chunk_len = ltoh16(header->chunk_len);
  253. int is_last = ltoh8(header->is_last);
  254. // check is_last field
  255. if (!(is_last == 0 || is_last == 1)) {
  256. PeerLog(o, BLOG_INFO, "chunk is_last wrong");
  257. break;
  258. }
  259. // obtain data
  260. if (o->in_len - o->in_pos < chunk_len) {
  261. PeerLog(o, BLOG_INFO, "too little data for chunk data");
  262. break;
  263. }
  264. // process chunk
  265. int res = process_chunk(o, frame_id, chunk_start, chunk_len, is_last, o->in + o->in_pos);
  266. o->in_pos += chunk_len;
  267. if (res) {
  268. // sending complete frame, stop processing input
  269. return;
  270. }
  271. }
  272. // increment packet time
  273. if (o->time == FPA_MAX_TIME) {
  274. reduce_times(o);
  275. if (!LinkedList2_IsEmpty(&o->frames_used)) {
  276. ASSERT(o->time < FPA_MAX_TIME) // If there was a frame with zero time, it was removed because
  277. // time_tolerance < FPA_MAX_TIME. So something >0 was subtracted.
  278. o->time++;
  279. } else {
  280. // it was set to zero by reduce_times
  281. ASSERT(o->time == 0)
  282. }
  283. } else {
  284. o->time++;
  285. }
  286. // set no input packet
  287. o->in_len = -1;
  288. // finish input
  289. PacketPassInterface_Done(&o->input);
  290. }
  291. static void input_handler_send (FragmentProtoAssembler *o, uint8_t *data, int data_len)
  292. {
  293. ASSERT(data_len >= 0)
  294. ASSERT(o->in_len == -1)
  295. DebugObject_Access(&o->d_obj);
  296. // save input packet
  297. o->in_len = data_len;
  298. o->in = data;
  299. o->in_pos = 0;
  300. process_input(o);
  301. }
  302. static void output_handler_done (FragmentProtoAssembler *o)
  303. {
  304. ASSERT(o->in_len >= 0)
  305. DebugObject_Access(&o->d_obj);
  306. process_input(o);
  307. }
  308. int FragmentProtoAssembler_Init (FragmentProtoAssembler *o, int input_mtu, PacketPassInterface *output, int num_frames, int num_chunks, BPendingGroup *pg, void *user, BLog_logfunc logfunc)
  309. {
  310. ASSERT(input_mtu >= 0)
  311. ASSERT(num_frames > 0)
  312. ASSERT(num_frames < FPA_MAX_TIME) // needed so we can always subtract times when packet time is maximum
  313. ASSERT(num_chunks > 0)
  314. // init arguments
  315. o->output = output;
  316. o->num_chunks = num_chunks;
  317. o->user = user;
  318. o->logfunc = logfunc;
  319. // init input
  320. PacketPassInterface_Init(&o->input, input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  321. // init output
  322. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  323. // remebmer output MTU
  324. o->output_mtu = PacketPassInterface_GetMTU(o->output);
  325. // set packet time to zero
  326. o->time = 0;
  327. // set time tolerance to num_frames
  328. o->time_tolerance = num_frames;
  329. // allocate frames
  330. if (!(o->frames_entries = BAllocArray(num_frames, sizeof(o->frames_entries[0])))) {
  331. goto fail1;
  332. }
  333. // allocate chunks
  334. if (!(o->frames_chunks = BAllocArray2(num_frames, o->num_chunks, sizeof(o->frames_chunks[0])))) {
  335. goto fail2;
  336. }
  337. // allocate buffers
  338. if (!(o->frames_buffer = BAllocArray(num_frames, o->output_mtu))) {
  339. goto fail3;
  340. }
  341. // init frame lists
  342. LinkedList2_Init(&o->frames_free);
  343. LinkedList2_Init(&o->frames_used);
  344. // initialize frame entries
  345. for (int i = 0; i < num_frames; i++) {
  346. struct FragmentProtoAssembler_frame *frame = &o->frames_entries[i];
  347. // set chunks array pointer
  348. frame->chunks = o->frames_chunks + (size_t)i * o->num_chunks;
  349. // set buffer pointer
  350. frame->buffer = o->frames_buffer + (size_t)i * o->output_mtu;
  351. // add to free list
  352. LinkedList2_Append(&o->frames_free, &frame->list_node);
  353. }
  354. // init tree
  355. BAVL_Init(&o->frames_used_tree, OFFSET_DIFF(struct FragmentProtoAssembler_frame, id, tree_node), (BAVL_comparator)frame_id_comparator, NULL);
  356. // have no input packet
  357. o->in_len = -1;
  358. DebugObject_Init(&o->d_obj);
  359. return 1;
  360. fail3:
  361. BFree(o->frames_chunks);
  362. fail2:
  363. BFree(o->frames_entries);
  364. fail1:
  365. PacketPassInterface_Free(&o->input);
  366. return 0;
  367. }
  368. void FragmentProtoAssembler_Free (FragmentProtoAssembler *o)
  369. {
  370. DebugObject_Free(&o->d_obj);
  371. // free buffers
  372. BFree(o->frames_buffer);
  373. // free chunks
  374. BFree(o->frames_chunks);
  375. // free frames
  376. BFree(o->frames_entries);
  377. // free input
  378. PacketPassInterface_Free(&o->input);
  379. }
  380. PacketPassInterface * FragmentProtoAssembler_GetInput (FragmentProtoAssembler *o)
  381. {
  382. DebugObject_Access(&o->d_obj);
  383. return &o->input;
  384. }