PacketProtoEncoder.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @file PacketProtoEncoder.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 <protocol/packetproto.h>
  24. #include <misc/balign.h>
  25. #include <misc/debug.h>
  26. #include <misc/byteorder.h>
  27. #include <flow/PacketProtoEncoder.h>
  28. static void output_handler_recv (PacketProtoEncoder *enc, uint8_t *data)
  29. {
  30. ASSERT(!enc->output_packet)
  31. ASSERT(data)
  32. DebugObject_Access(&enc->d_obj);
  33. // schedule receive
  34. enc->output_packet = data;
  35. PacketRecvInterface_Receiver_Recv(enc->input, enc->output_packet + sizeof(struct packetproto_header));
  36. }
  37. static void input_handler_done (PacketProtoEncoder *enc, int in_len)
  38. {
  39. ASSERT(enc->output_packet)
  40. DebugObject_Access(&enc->d_obj);
  41. // write length
  42. ((struct packetproto_header *)enc->output_packet)->len = htol16(in_len);
  43. // finish output packet
  44. enc->output_packet = NULL;
  45. PacketRecvInterface_Done(&enc->output, PACKETPROTO_ENCLEN(in_len));
  46. }
  47. void PacketProtoEncoder_Init (PacketProtoEncoder *enc, PacketRecvInterface *input, BPendingGroup *pg)
  48. {
  49. ASSERT(PacketRecvInterface_GetMTU(input) <= PACKETPROTO_MAXPAYLOAD)
  50. // init arguments
  51. enc->input = input;
  52. // init input
  53. PacketRecvInterface_Receiver_Init(enc->input, (PacketRecvInterface_handler_done)input_handler_done, enc);
  54. // init output
  55. PacketRecvInterface_Init(
  56. &enc->output, PACKETPROTO_ENCLEN(PacketRecvInterface_GetMTU(enc->input)),
  57. (PacketRecvInterface_handler_recv)output_handler_recv, enc, pg
  58. );
  59. // set no output packet
  60. enc->output_packet = NULL;
  61. DebugObject_Init(&enc->d_obj);
  62. }
  63. void PacketProtoEncoder_Free (PacketProtoEncoder *enc)
  64. {
  65. DebugObject_Free(&enc->d_obj);
  66. // free input
  67. PacketRecvInterface_Free(&enc->output);
  68. }
  69. PacketRecvInterface * PacketProtoEncoder_GetOutput (PacketProtoEncoder *enc)
  70. {
  71. DebugObject_Access(&enc->d_obj);
  72. return &enc->output;
  73. }