PacketBufferAsyncInput.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file PacketBufferAsyncInput.h
  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. * @section DESCRIPTION
  23. *
  24. * Object for writing packets to a {@link PacketRecvInterface} client
  25. * via {@link BestEffortPacketWriteInterface}.
  26. */
  27. #ifndef BADVPN_FLOW_PACKETBUFFERASYNCINPUT_H
  28. #define BADVPN_FLOW_PACKETBUFFERASYNCINPUT_H
  29. #include <stdint.h>
  30. #include <misc/debug.h>
  31. #include <system/DebugObject.h>
  32. #include <flow/PacketRecvInterface.h>
  33. #include <flow/BestEffortPacketWriteInterface.h>
  34. typedef void (*PacketBufferAsyncInput_handler_keepalive) (void *user);
  35. /**
  36. * Object for writing packets to a {@link PacketRecvInterface} client
  37. * via {@link BestEffortPacketWriteInterface}.
  38. */
  39. typedef struct {
  40. DebugObject d_obj;
  41. BestEffortPacketWriteInterface input;
  42. PacketRecvInterface recv_interface;
  43. int have_output_packet;
  44. uint8_t *output_packet;
  45. } PacketBufferAsyncInput;
  46. /**
  47. * Initializes the object.
  48. *
  49. * @param f the object
  50. */
  51. static void PacketBufferAsyncInput_Init (PacketBufferAsyncInput *f, int mtu);
  52. /**
  53. * Frees the object.
  54. *
  55. * @param f the object
  56. */
  57. static void PacketBufferAsyncInput_Free (PacketBufferAsyncInput *f);
  58. /**
  59. * Returns the output interface.
  60. *
  61. * @param f the object
  62. * @return output interface
  63. */
  64. static PacketRecvInterface * PacketBufferAsyncInput_GetOutput (PacketBufferAsyncInput *f);
  65. /**
  66. * Returns the input interface.
  67. *
  68. * @param f the object
  69. * @return input interface
  70. */
  71. static BestEffortPacketWriteInterface * PacketBufferAsyncInput_GetInput (PacketBufferAsyncInput *f);
  72. static int _PacketBufferAsyncInput_output_handler_recv (PacketBufferAsyncInput *f, uint8_t *data, int *data_len)
  73. {
  74. ASSERT(!f->have_output_packet)
  75. // store destination
  76. f->have_output_packet = 1;
  77. f->output_packet = data;
  78. // block
  79. return 0;
  80. }
  81. static int _PacketBufferAsyncInput_handler_startpacket (PacketBufferAsyncInput *f, uint8_t **data)
  82. {
  83. if (!f->have_output_packet) {
  84. // buffer full
  85. return 0;
  86. }
  87. if (data) {
  88. *data = f->output_packet;
  89. }
  90. return 1;
  91. }
  92. static void _PacketBufferAsyncInput_handler_endpacket (PacketBufferAsyncInput *f, int len)
  93. {
  94. f->have_output_packet = 0;
  95. PacketRecvInterface_Done(&f->recv_interface, len);
  96. return;
  97. }
  98. void PacketBufferAsyncInput_Init (PacketBufferAsyncInput *f, int mtu)
  99. {
  100. ASSERT(mtu >= 0)
  101. PacketRecvInterface_Init(
  102. &f->recv_interface,
  103. mtu,
  104. (PacketRecvInterface_handler_recv)_PacketBufferAsyncInput_output_handler_recv,
  105. f
  106. );
  107. BestEffortPacketWriteInterface_Init(
  108. &f->input,
  109. mtu,
  110. (BestEffortPacketWriteInterface_handler_startpacket)_PacketBufferAsyncInput_handler_startpacket,
  111. (BestEffortPacketWriteInterface_handler_endpacket)_PacketBufferAsyncInput_handler_endpacket,
  112. f
  113. );
  114. f->have_output_packet = 0;
  115. // init debug object
  116. DebugObject_Init(&f->d_obj);
  117. }
  118. void PacketBufferAsyncInput_Free (PacketBufferAsyncInput *f)
  119. {
  120. // free debug object
  121. DebugObject_Free(&f->d_obj);
  122. BestEffortPacketWriteInterface_Free(&f->input);
  123. PacketRecvInterface_Free(&f->recv_interface);
  124. }
  125. PacketRecvInterface * PacketBufferAsyncInput_GetOutput (PacketBufferAsyncInput *f)
  126. {
  127. return &f->recv_interface;
  128. }
  129. BestEffortPacketWriteInterface * PacketBufferAsyncInput_GetInput (PacketBufferAsyncInput *f)
  130. {
  131. return &f->input;
  132. }
  133. #endif