PacketProtoEncoder.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 int encode_packet (PacketProtoEncoder *enc, uint8_t *data, int in_len);
  29. static void output_handler_recv (PacketProtoEncoder *enc, uint8_t *data);
  30. static void input_handler_done (PacketProtoEncoder *enc, int in_len);
  31. int encode_packet (PacketProtoEncoder *enc, uint8_t *data, int in_len)
  32. {
  33. // write header
  34. struct packetproto_header *header = (struct packetproto_header *)data;
  35. header->len = htol16(in_len);
  36. return PACKETPROTO_ENCLEN(in_len);
  37. }
  38. void output_handler_recv (PacketProtoEncoder *enc, uint8_t *data)
  39. {
  40. ASSERT(!enc->output_packet)
  41. ASSERT(data)
  42. DebugObject_Access(&enc->d_obj);
  43. // schedule receive
  44. PacketRecvInterface_Receiver_Recv(enc->input, data + sizeof(struct packetproto_header));
  45. enc->output_packet = data;
  46. }
  47. void input_handler_done (PacketProtoEncoder *enc, int in_len)
  48. {
  49. ASSERT(enc->output_packet)
  50. DebugObject_Access(&enc->d_obj);
  51. // encode
  52. int out_len = encode_packet(enc, enc->output_packet, in_len);
  53. // finish output packet
  54. enc->output_packet = NULL;
  55. PacketRecvInterface_Done(&enc->output, out_len);
  56. }
  57. void PacketProtoEncoder_Init (PacketProtoEncoder *enc, PacketRecvInterface *input, BPendingGroup *pg)
  58. {
  59. ASSERT(PacketRecvInterface_GetMTU(input) <= PACKETPROTO_MAXPAYLOAD)
  60. // init arguments
  61. enc->input = input;
  62. // init input
  63. PacketRecvInterface_Receiver_Init(enc->input, (PacketRecvInterface_handler_done)input_handler_done, enc);
  64. // init output
  65. PacketRecvInterface_Init(
  66. &enc->output,
  67. PACKETPROTO_ENCLEN(PacketRecvInterface_GetMTU(enc->input)),
  68. (PacketRecvInterface_handler_recv)output_handler_recv,
  69. enc,
  70. pg
  71. );
  72. // set no output packet
  73. enc->output_packet = NULL;
  74. DebugObject_Init(&enc->d_obj);
  75. }
  76. void PacketProtoEncoder_Free (PacketProtoEncoder *enc)
  77. {
  78. DebugObject_Free(&enc->d_obj);
  79. // free input
  80. PacketRecvInterface_Free(&enc->output);
  81. }
  82. PacketRecvInterface * PacketProtoEncoder_GetOutput (PacketProtoEncoder *enc)
  83. {
  84. DebugObject_Access(&enc->d_obj);
  85. return &enc->output;
  86. }