DataProto.h 9.9 KB

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