PacketProtoEncoder.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 int output_handler_recv (PacketProtoEncoder *enc, uint8_t *data, int *out_len);
  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. int output_handler_recv (PacketProtoEncoder *enc, uint8_t *data, int *out_len)
  39. {
  40. ASSERT(!enc->output_packet)
  41. ASSERT(data)
  42. // call recv on input
  43. int in_len;
  44. DEAD_ENTER(enc->dead)
  45. int res = PacketRecvInterface_Receiver_Recv(enc->input, data + sizeof(struct packetproto_header), &in_len);
  46. if (DEAD_LEAVE(enc->dead)) {
  47. return -1;
  48. }
  49. ASSERT(res == 0 || res == 1)
  50. if (!res) {
  51. // input busy, continue in input_handler_done
  52. enc->output_packet = data;
  53. return 0;
  54. }
  55. // encode
  56. *out_len = encode_packet(enc, data, in_len);
  57. return 1;
  58. }
  59. void input_handler_done (PacketProtoEncoder *enc, int in_len)
  60. {
  61. ASSERT(enc->output_packet)
  62. // encode
  63. int out_len = encode_packet(enc, enc->output_packet, in_len);
  64. // set no output packet
  65. enc->output_packet = NULL;
  66. // notify output
  67. PacketRecvInterface_Done(&enc->output, out_len);
  68. return;
  69. }
  70. void PacketProtoEncoder_Init (PacketProtoEncoder *enc, PacketRecvInterface *input)
  71. {
  72. ASSERT(PacketRecvInterface_GetMTU(input) <= PACKETPROTO_MAXPAYLOAD)
  73. // init arguments
  74. enc->input = input;
  75. // init dead var
  76. DEAD_INIT(enc->dead);
  77. // init input
  78. PacketRecvInterface_Receiver_Init(enc->input, (PacketRecvInterface_handler_done)input_handler_done, enc);
  79. // init output
  80. PacketRecvInterface_Init(
  81. &enc->output,
  82. PACKETPROTO_ENCLEN(PacketRecvInterface_GetMTU(enc->input)),
  83. (PacketRecvInterface_handler_recv)output_handler_recv,
  84. enc
  85. );
  86. // set no output packet
  87. enc->output_packet = NULL;
  88. // init debug object
  89. DebugObject_Init(&enc->d_obj);
  90. }
  91. void PacketProtoEncoder_Free (PacketProtoEncoder *enc)
  92. {
  93. // free debug object
  94. DebugObject_Free(&enc->d_obj);
  95. // free input
  96. PacketRecvInterface_Free(&enc->output);
  97. // free dead var
  98. DEAD_KILL(enc->dead);
  99. }
  100. PacketRecvInterface * PacketProtoEncoder_GetOutput (PacketProtoEncoder *enc)
  101. {
  102. return &enc->output;
  103. }