DataProto.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @file DataProto.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. * Mudule for frame sending used in the VPN client program.
  25. */
  26. #ifndef BADVPN_CLIENT_DATAPROTO_H
  27. #define BADVPN_CLIENT_DATAPROTO_H
  28. #include <stdint.h>
  29. #include <misc/debugcounter.h>
  30. #include <misc/debug.h>
  31. #include <base/DebugObject.h>
  32. #include <system/BReactor.h>
  33. #include <flow/PacketPassFairQueue.h>
  34. #include <flow/PacketPassNotifier.h>
  35. #include <flow/PacketRecvBlocker.h>
  36. #include <flow/SinglePacketBuffer.h>
  37. #include <flow/PacketPassConnector.h>
  38. #include <flow/PacketRouter.h>
  39. #include <flowextra/PacketPassInactivityMonitor.h>
  40. #include <client/DataProtoKeepaliveSource.h>
  41. typedef void (*DataProtoSink_handler) (void *user, int up);
  42. typedef void (*DataProtoSource_handler) (void *user, const uint8_t *frame, int frame_len);
  43. typedef void (*DataProtoFlow_handler_inactivity) (void *user);
  44. struct DataProtoFlow_buffer;
  45. /**
  46. * Frame destination.
  47. * Represents a peer as a destination for sending frames to.
  48. */
  49. typedef struct {
  50. BReactor *reactor;
  51. int frame_mtu;
  52. PacketPassFairQueue queue;
  53. PacketPassInactivityMonitor monitor;
  54. PacketPassNotifier notifier;
  55. DataProtoKeepaliveSource ka_source;
  56. PacketRecvBlocker ka_blocker;
  57. SinglePacketBuffer ka_buffer;
  58. PacketPassFairQueueFlow ka_qflow;
  59. BTimer receive_timer;
  60. int up;
  61. int up_report;
  62. DataProtoSink_handler handler;
  63. void *user;
  64. BPending up_job;
  65. struct DataProtoFlow_buffer *detaching_buffer;
  66. DebugObject d_obj;
  67. DebugCounter d_ctr;
  68. } DataProtoSink;
  69. /**
  70. * Receives frames from a {@link PacketRecvInterface} input and
  71. * allows the user to route them to buffers in {@link DataProtoFlow} objects.
  72. */
  73. typedef struct {
  74. DataProtoSource_handler handler;
  75. void *user;
  76. BReactor *reactor;
  77. int frame_mtu;
  78. PacketRouter router;
  79. uint8_t *current_buf;
  80. int current_recv_len;
  81. DebugObject d_obj;
  82. DebugCounter d_ctr;
  83. } DataProtoSource;
  84. /**
  85. * Contains a buffer for frames from a specific peer to a specific peer.
  86. * Receives frames from a {@link DataProtoSource} as routed by the user.
  87. * Can be attached to a {@link DataProtoSink} to send out frames.
  88. */
  89. typedef struct {
  90. DataProtoSource *source;
  91. peerid_t source_id;
  92. peerid_t dest_id;
  93. DataProtoSink *dp_desired;
  94. struct DataProtoFlow_buffer *b;
  95. DebugObject d_obj;
  96. } DataProtoFlow;
  97. struct DataProtoFlow_buffer {
  98. DataProtoFlow *flow;
  99. int inactivity_time;
  100. RouteBuffer rbuf;
  101. PacketPassInactivityMonitor monitor;
  102. PacketPassConnector connector;
  103. DataProtoSink *dp;
  104. PacketPassFairQueueFlow dp_qflow;
  105. };
  106. /**
  107. * Initializes the object.
  108. *
  109. * @param o the object
  110. * @param reactor reactor we live in
  111. * @param output output interface. Must support cancel functionality. Its MTU must be
  112. * >=DATAPROTO_MAX_OVERHEAD.
  113. * @param keepalive_time keepalive time
  114. * @param tolerance_time after how long of not having received anything from the peer
  115. * to consider the link down
  116. * @param handler up state handler
  117. * @param user value to pass to handler
  118. * @return 1 on success, 0 on failure
  119. */
  120. int DataProtoSink_Init (DataProtoSink *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoSink_handler handler, void *user) WARN_UNUSED;
  121. /**
  122. * Frees the object.
  123. * There must be no local sources attached.
  124. *
  125. * @param o the object
  126. */
  127. void DataProtoSink_Free (DataProtoSink *o);
  128. /**
  129. * Notifies the object that a packet was received from the peer.
  130. * Must not be in freeing state.
  131. *
  132. * @param o the object
  133. * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
  134. * Must be 0 or 1.
  135. */
  136. void DataProtoSink_Received (DataProtoSink *o, int peer_receiving);
  137. /**
  138. * Initiazes the object.
  139. *
  140. * @param o the object
  141. * @param input frame input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
  142. * @param handler handler called when a frame arrives to allow the user to route it to
  143. * appropriate {@link DataProtoFlow} objects.
  144. * @param user value passed to handler
  145. * @param reactor reactor we live in
  146. * @return 1 on success, 0 on failure
  147. */
  148. int DataProtoSource_Init (DataProtoSource *o, PacketRecvInterface *input, DataProtoSource_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  149. /**
  150. * Frees the object.
  151. * There must be no {@link DataProtoFlow} objects using this source.
  152. *
  153. * @param o the object
  154. */
  155. void DataProtoSource_Free (DataProtoSource *o);
  156. /**
  157. * Initializes the object.
  158. * The object is initialized in not attached state.
  159. *
  160. * @param o the object
  161. * @param source source to receive frames from
  162. * @param source_id source peer ID to encode in the headers (i.e. our ID)
  163. * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
  164. * object belongs to)
  165. * @param num_packets number of packets the buffer should hold. Must be >0.
  166. * @param inactivity_time milliseconds of output inactivity after which to call the
  167. * inactivity handler; <0 to disable. Note that the object is considered
  168. * active as long as its buffer is non-empty, even if is not attached to
  169. * a {@link DataProtoSink}.
  170. * @param handler_inactivity inactivity handler, if inactivity_time >=0
  171. * @param user value to pass to handler
  172. * @return 1 on success, 0 on failure
  173. */
  174. int DataProtoFlow_Init (
  175. DataProtoFlow *o, DataProtoSource *source, peerid_t source_id, peerid_t dest_id, int num_packets,
  176. int inactivity_time, DataProtoFlow_handler_inactivity handler_inactivity, void *user
  177. ) WARN_UNUSED;
  178. /**
  179. * Frees the object.
  180. * The object must be in not attached state.
  181. *
  182. * @param o the object
  183. */
  184. void DataProtoFlow_Free (DataProtoFlow *o);
  185. /**
  186. * Routes a frame from the flow's source to this flow.
  187. * Must be called from within the job context of the {@link DataProtoSource_handler} handler.
  188. * Must not be called after this has been called with more=0 for the current frame.
  189. *
  190. * @param o the object
  191. * @param more whether the current frame may have to be routed to more
  192. * objects. If 0, must not be called again until the handler is
  193. * called for the next frame. Must be 0 or 1.
  194. */
  195. void DataProtoFlow_Route (DataProtoFlow *o, int more);
  196. /**
  197. * Attaches the object to a destination.
  198. * The object must be in not attached state.
  199. *
  200. * @param o the object
  201. * @param dp destination to attach to. This object's frame_mtu must be <=
  202. * (output MTU of dp) - DATAPROTO_MAX_OVERHEAD.
  203. */
  204. void DataProtoFlow_Attach (DataProtoFlow *o, DataProtoSink *dp);
  205. /**
  206. * Detaches the object from a destination.
  207. * The object must be in attached state.
  208. *
  209. * @param o the object
  210. */
  211. void DataProtoFlow_Detach (DataProtoFlow *o);
  212. #endif