DataProto.h 9.8 KB

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