DataProto.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <protocol/scproto.h>
  30. #include <misc/debugcounter.h>
  31. #include <misc/debug.h>
  32. #include <structure/LinkedList2.h>
  33. #include <structure/BAVL.h>
  34. #include <system/DebugObject.h>
  35. #include <system/BReactor.h>
  36. #include <system/BPending.h>
  37. #include <flow/PacketPassFairQueue.h>
  38. #include <flow/PacketPassInactivityMonitor.h>
  39. #include <flow/PacketPassNotifier.h>
  40. #include <flow/DataProtoKeepaliveSource.h>
  41. #include <flow/PacketRecvBlocker.h>
  42. #include <flow/SinglePacketBuffer.h>
  43. #include <flow/BufferWriter.h>
  44. #include <flow/PacketBuffer.h>
  45. #include <flow/PacketPassConnector.h>
  46. #include <flow/PacketRouter.h>
  47. typedef void (*DataProtoDest_handler) (void *user, int up);
  48. typedef void (*DataProtoDevice_handler) (void *user, const uint8_t *frame, int frame_len);
  49. struct dp_relay_flow;
  50. /**
  51. * Frame destination.
  52. * Represents a peer as a destination for sending frames to.
  53. */
  54. typedef struct {
  55. BReactor *reactor;
  56. peerid_t dest_id;
  57. int mtu;
  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. DataProtoDest_handler handler;
  69. void *user;
  70. LinkedList2 relay_flows_list;
  71. BPending keepalive_job;
  72. int freeing;
  73. DebugCounter flows_counter;
  74. DebugObject d_obj;
  75. #ifndef NDEBUG
  76. PacketPassInterface *d_output;
  77. #endif
  78. } DataProtoDest;
  79. /**
  80. * Object that receives frames from a device and routes
  81. * them to buffers in {@link DataProtoLocalSource} objects.
  82. */
  83. typedef struct {
  84. DataProtoDevice_handler handler;
  85. void *user;
  86. BReactor *reactor;
  87. int frame_mtu;
  88. PacketRouter router;
  89. uint8_t *current_buf;
  90. int current_recv_len;
  91. DebugObject d_obj;
  92. DebugCounter d_ctr;
  93. } DataProtoDevice;
  94. /**
  95. * Local frame source.
  96. * Buffers frames received from the TAP device, addressed to a particular peer.
  97. */
  98. typedef struct {
  99. DataProtoDevice *device;
  100. peerid_t source_id;
  101. peerid_t dest_id;
  102. RouteBuffer rbuf;
  103. PacketPassConnector connector;
  104. DataProtoDest *dp;
  105. PacketPassFairQueueFlow dp_qflow;
  106. DebugObject d_obj;
  107. } DataProtoLocalSource;
  108. /**
  109. * Relay frame source.
  110. * Represents relaying of frames from one particular peer to other peers.
  111. */
  112. typedef struct {
  113. peerid_t source_id;
  114. LinkedList2 relay_flows_list;
  115. BAVL relay_flows_tree;
  116. DebugObject d_obj;
  117. } DataProtoRelaySource;
  118. struct dp_relay_flow {
  119. DataProtoRelaySource *rs;
  120. DataProtoDest *dp;
  121. BufferWriter ainput;
  122. PacketBuffer buffer;
  123. PacketPassInactivityMonitor monitor;
  124. PacketPassFairQueueFlow qflow;
  125. LinkedList2Node source_list_node;
  126. BAVLNode source_tree_node;
  127. LinkedList2Node dp_list_node;
  128. BPending first_frame_job;
  129. uint8_t *first_frame;
  130. int first_frame_len;
  131. };
  132. /**
  133. * Initializes the object.
  134. *
  135. * @param o the object
  136. * @param reactor reactor we live in
  137. * @param dest_id ID of the peer this object sends to
  138. * @param output output interface. Must support cancel functionality. Its MTU must be
  139. * >=sizeof(struct dataproto_header)+sizeof(struct dataproto_peer_id).
  140. * @param keepalive_time keepalive time
  141. * @param tolerance_time after how long of not having received anything from the peer
  142. * to consider the link down
  143. * @param handler up state handler
  144. * @param user value to pass to handler
  145. * @return 1 on success, 0 on failure
  146. */
  147. int DataProtoDest_Init (DataProtoDest *o, BReactor *reactor, peerid_t dest_id, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoDest_handler handler, void *user) WARN_UNUSED;
  148. /**
  149. * Frees the object.
  150. * There must be no local sources attached.
  151. *
  152. * @param o the object
  153. */
  154. void DataProtoDest_Free (DataProtoDest *o);
  155. /**
  156. * Prepares for freeing the object by allowing freeing of local sources.
  157. * The object enters freeing state.
  158. * The object must be freed before returning control to the reactor,
  159. * and before any further I/O (output or submitting frames).
  160. *
  161. * @param o the object
  162. */
  163. void DataProtoDest_PrepareFree (DataProtoDest *o);
  164. /**
  165. * Submits a relayed frame.
  166. * Must not be in freeing state.
  167. * Must not be called before it evaluates.
  168. *
  169. * @param o the object
  170. * @param rs relay source object representing the peer this frame came from
  171. * @param data frame data. The frame must remain accessible until this evaluates.
  172. * @param data_len frame length. Must be >=0.
  173. * Must be <= (output MTU) - (sizeof(struct dataproto_header) + sizeof(struct dataproto_peer_id)).
  174. * @param buffer_num_packets number of packets the relay buffer should hold, in case it doesn't exist.
  175. * Must be >0.
  176. */
  177. void DataProtoDest_SubmitRelayFrame (DataProtoDest *o, DataProtoRelaySource *rs, uint8_t *data, int data_len, int buffer_num_packets);
  178. /**
  179. * Notifies the object that a packet was received from the peer.
  180. * Must not be in freeing state.
  181. * Must not be called from output Send calls.
  182. * May call the up state handler.
  183. * May invoke output I/O.
  184. *
  185. * @param o the object
  186. * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
  187. * Must be 0 or 1.
  188. */
  189. void DataProtoDest_Received (DataProtoDest *o, int peer_receiving);
  190. /**
  191. * Initiazes the object.
  192. *
  193. * @param o the object
  194. * @param input device input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
  195. * @param handler handler called when a packet arrives to allow the user to route it to
  196. * appropriate {@link DataProtoLocalSource} objects.
  197. * @param user value passed to handler
  198. * @param reactor reactor we live in
  199. * @return 1 on success, 0 on failure
  200. */
  201. int DataProtoDevice_Init (DataProtoDevice *o, PacketRecvInterface *input, DataProtoDevice_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  202. /**
  203. * Frees the object.
  204. * There must be no {@link DataProtoLocalSource} objects referring to this device.
  205. *
  206. * @param o the object
  207. */
  208. void DataProtoDevice_Free (DataProtoDevice *o);
  209. /**
  210. * Initializes the object.
  211. * The object is initialized in not attached state.
  212. *
  213. * @param o the object
  214. * @param device device to receive frames from
  215. * @param source_id source peer ID to encode in the headers (i.e. our ID)
  216. * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
  217. * object belongs to)
  218. * @param num_packets number of packets the buffer should hold. Must be >0.
  219. * @return 1 on success, 0 on failure
  220. */
  221. int DataProtoLocalSource_Init (DataProtoLocalSource *o, DataProtoDevice *device, peerid_t source_id, peerid_t dest_id, int num_packets) WARN_UNUSED;
  222. /**
  223. * Frees the object.
  224. * The object must be in not attached state.
  225. *
  226. * @param o the object
  227. */
  228. void DataProtoLocalSource_Free (DataProtoLocalSource *o);
  229. /**
  230. * Routes a frame from the device to this object.
  231. * Must be called from within the job context of the {@link DataProtoDevice_handler} handler.
  232. * Must not be called after this has been called with more=0 for the current frame.
  233. *
  234. * @param o the object
  235. * @param more whether the current frame may have to be routed to more
  236. * objects. If 0, must not be called again until the handler is
  237. * called for the next frame. Must be 0 or 1.
  238. */
  239. void DataProtoLocalSource_Route (DataProtoLocalSource *o, int more);
  240. /**
  241. * Attaches the object to a destination.
  242. * The object must be in not attached state.
  243. *
  244. * @param o the object
  245. * @param dp destination to attach to. This object's frame_mtu must be <= destination's
  246. * (output MTU)-(sizeof(struct dataproto_header)+sizeof(struct dataproto_peer_id)).
  247. */
  248. void DataProtoLocalSource_Attach (DataProtoLocalSource *o, DataProtoDest *dp);
  249. /**
  250. * Detaches the object from a destination.
  251. * The object must be in attached state.
  252. * Unless the destination is in freeing state, must not be called from destination's
  253. * output Send calls.
  254. *
  255. * @param o the object
  256. */
  257. void DataProtoLocalSource_Detach (DataProtoLocalSource *o);
  258. /**
  259. * Initializes the object
  260. *
  261. * @param o the object
  262. * @param source_id ID of the peer whose relayed frames this object represents
  263. */
  264. void DataProtoRelaySource_Init (DataProtoRelaySource *o, peerid_t source_id);
  265. /**
  266. * Frees the object.
  267. * The object must have no relay flows (guaranteed if no frames have been submitted
  268. * with it using {@link DataProtoDest_SubmitRelayFrame}).
  269. *
  270. * @param o the object
  271. */
  272. void DataProtoRelaySource_Free (DataProtoRelaySource *o);
  273. /**
  274. * Asserts that the object can be freed.
  275. *
  276. * @param o the object
  277. * @return 1 if there are no relay flows, 0 if at least one
  278. */
  279. void DataProtoRelaySource_AssertFree (DataProtoRelaySource *o);
  280. /**
  281. * Removes all relay flows by releasing them.
  282. * None of the destinations must be in freeing state.
  283. * Must not be called from any of the destinations' output Send calls.
  284. * May invoke the destinations' output Cancel calls.
  285. *
  286. * @param o the object
  287. */
  288. void DataProtoRelaySource_Release (DataProtoRelaySource *o);
  289. /**
  290. * Removes all relay flows by putting destinations into freeing state.
  291. * May put destinations into freeing state (as if {@link DataProtoDest_PrepareFree}
  292. * was called on them).
  293. * This should only be used while freeing the entire frame sending system.
  294. *
  295. * @param o the object
  296. */
  297. void DataProtoRelaySource_FreeRelease (DataProtoRelaySource *o);
  298. #endif