DataProto.h 9.5 KB

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