DataProto.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @file DataProto.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Mudule for frame sending used in the VPN client program.
  32. */
  33. #ifndef BADVPN_CLIENT_DATAPROTO_H
  34. #define BADVPN_CLIENT_DATAPROTO_H
  35. #include <stdint.h>
  36. #include <misc/debugcounter.h>
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. #include <system/BReactor.h>
  40. #include <flow/PacketPassFairQueue.h>
  41. #include <flow/PacketPassNotifier.h>
  42. #include <flow/PacketRecvBlocker.h>
  43. #include <flow/SinglePacketBuffer.h>
  44. #include <flow/PacketPassConnector.h>
  45. #include <flow/PacketRouter.h>
  46. #include <flowextra/PacketPassInactivityMonitor.h>
  47. #include <client/DataProtoKeepaliveSource.h>
  48. typedef void (*DataProtoSink_handler) (void *user, int up);
  49. typedef void (*DataProtoSource_handler) (void *user, const uint8_t *frame, int frame_len);
  50. typedef void (*DataProtoFlow_handler_inactivity) (void *user);
  51. struct DataProtoFlow_buffer;
  52. /**
  53. * Frame destination.
  54. * Represents a peer as a destination for sending frames to.
  55. */
  56. typedef struct {
  57. BReactor *reactor;
  58. int frame_mtu;
  59. PacketPassFairQueue queue;
  60. PacketPassInactivityMonitor monitor;
  61. PacketPassNotifier notifier;
  62. DataProtoKeepaliveSource ka_source;
  63. PacketRecvBlocker ka_blocker;
  64. SinglePacketBuffer ka_buffer;
  65. PacketPassFairQueueFlow ka_qflow;
  66. BTimer receive_timer;
  67. int up;
  68. int up_report;
  69. DataProtoSink_handler handler;
  70. void *user;
  71. BPending up_job;
  72. struct DataProtoFlow_buffer *detaching_buffer;
  73. DebugObject d_obj;
  74. DebugCounter d_ctr;
  75. } DataProtoSink;
  76. /**
  77. * Receives frames from a {@link PacketRecvInterface} input and
  78. * allows the user to route them to buffers in {@link DataProtoFlow}'s.
  79. */
  80. typedef struct {
  81. DataProtoSource_handler handler;
  82. void *user;
  83. BReactor *reactor;
  84. int frame_mtu;
  85. PacketRouter router;
  86. uint8_t *current_buf;
  87. int current_recv_len;
  88. DebugObject d_obj;
  89. DebugCounter d_ctr;
  90. } DataProtoSource;
  91. /**
  92. * Contains a buffer for frames from a specific peer to a specific peer.
  93. * Receives frames from a {@link DataProtoSource} as routed by the user.
  94. * Can be attached to a {@link DataProtoSink} to send out frames.
  95. */
  96. typedef struct {
  97. DataProtoSource *source;
  98. peerid_t source_id;
  99. peerid_t dest_id;
  100. DataProtoSink *sink_desired;
  101. struct DataProtoFlow_buffer *b;
  102. DebugObject d_obj;
  103. } DataProtoFlow;
  104. struct DataProtoFlow_buffer {
  105. DataProtoFlow *flow;
  106. int inactivity_time;
  107. RouteBuffer rbuf;
  108. PacketPassInactivityMonitor monitor;
  109. PacketPassConnector connector;
  110. DataProtoSink *sink;
  111. PacketPassFairQueueFlow sink_qflow;
  112. };
  113. /**
  114. * Initializes the sink.
  115. *
  116. * @param o the object
  117. * @param reactor reactor we live in
  118. * @param output output interface. Must support cancel functionality. Its MTU must be
  119. * >=DATAPROTO_MAX_OVERHEAD.
  120. * @param keepalive_time keepalive time
  121. * @param tolerance_time after how long of not having received anything from the peer
  122. * to consider the link down
  123. * @param handler up state handler
  124. * @param user value to pass to handler
  125. * @return 1 on success, 0 on failure
  126. */
  127. int DataProtoSink_Init (DataProtoSink *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoSink_handler handler, void *user) WARN_UNUSED;
  128. /**
  129. * Frees the sink.
  130. * There must be no local sources attached.
  131. *
  132. * @param o the object
  133. */
  134. void DataProtoSink_Free (DataProtoSink *o);
  135. /**
  136. * Notifies the sink that a packet was received from the peer.
  137. * Must not be in freeing state.
  138. *
  139. * @param o the object
  140. * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
  141. * Must be 0 or 1.
  142. */
  143. void DataProtoSink_Received (DataProtoSink *o, int peer_receiving);
  144. /**
  145. * Initiazes the source.
  146. *
  147. * @param o the object
  148. * @param input frame input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
  149. * @param handler handler called when a frame arrives to allow the user to route it to
  150. * appropriate {@link DataProtoFlow}'s.
  151. * @param user value passed to handler
  152. * @param reactor reactor we live in
  153. * @return 1 on success, 0 on failure
  154. */
  155. int DataProtoSource_Init (DataProtoSource *o, PacketRecvInterface *input, DataProtoSource_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  156. /**
  157. * Frees the source.
  158. * There must be no {@link DataProtoFlow}'s using this source.
  159. *
  160. * @param o the object
  161. */
  162. void DataProtoSource_Free (DataProtoSource *o);
  163. /**
  164. * Initializes the flow.
  165. * The flow is initialized in not attached state.
  166. *
  167. * @param o the object
  168. * @param source source to receive frames from
  169. * @param source_id source peer ID to encode in the headers (i.e. our ID)
  170. * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
  171. * flow belongs to)
  172. * @param num_packets number of packets the buffer should hold. Must be >0.
  173. * @param inactivity_time milliseconds of output inactivity after which to call the
  174. * inactivity handler; <0 to disable. Note that the flow is considered
  175. * active as long as its buffer is non-empty, even if is not attached to
  176. * a {@link DataProtoSink}.
  177. * @param user value to pass to handler
  178. * @param handler_inactivity inactivity handler, if inactivity_time >=0
  179. * @return 1 on success, 0 on failure
  180. */
  181. int DataProtoFlow_Init (DataProtoFlow *o, DataProtoSource *source, peerid_t source_id, peerid_t dest_id, int num_packets, int inactivity_time, void *user,
  182. DataProtoFlow_handler_inactivity handler_inactivity) WARN_UNUSED;
  183. /**
  184. * Frees the flow.
  185. * The flow must be in not attached state.
  186. *
  187. * @param o the object
  188. */
  189. void DataProtoFlow_Free (DataProtoFlow *o);
  190. /**
  191. * Routes a frame from the flow's source to this flow.
  192. * Must be called from within the job context of the {@link DataProtoSource_handler} handler.
  193. * Must not be called after this has been called with more=0 for the current frame.
  194. *
  195. * @param o the object
  196. * @param more whether the current frame may have to be routed to more
  197. * flows. If 0, must not be called again until the handler is
  198. * called for the next frame. Must be 0 or 1.
  199. */
  200. void DataProtoFlow_Route (DataProtoFlow *o, int more);
  201. /**
  202. * Attaches the flow to a sink.
  203. * The flow must be in not attached state.
  204. *
  205. * @param o the object
  206. * @param sink sink to attach to. This flow's frame_mtu must be <=
  207. * (output MTU of sink) - DATAPROTO_MAX_OVERHEAD.
  208. */
  209. void DataProtoFlow_Attach (DataProtoFlow *o, DataProtoSink *sink);
  210. /**
  211. * Detaches the flow from a destination.
  212. * The flow must be in attached state.
  213. *
  214. * @param o the object
  215. */
  216. void DataProtoFlow_Detach (DataProtoFlow *o);
  217. #endif