DataProto.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <system/DebugObject.h>
  32. #include <system/BReactor.h>
  33. #include <flow/PacketPassFairQueue.h>
  34. #include <flow/PacketPassInactivityMonitor.h>
  35. #include <flow/PacketPassNotifier.h>
  36. #include <flow/DataProtoKeepaliveSource.h>
  37. #include <flow/PacketRecvBlocker.h>
  38. #include <flow/SinglePacketBuffer.h>
  39. #include <flow/PacketPassConnector.h>
  40. #include <flow/PacketRouter.h>
  41. typedef void (*DataProtoDest_handler) (void *user, int up);
  42. typedef void (*DataProtoDevice_handler) (void *user, const uint8_t *frame, int frame_len);
  43. typedef void (*DataProtoLocalSource_handler_inactivity) (void *user);
  44. /**
  45. * Frame destination.
  46. * Represents a peer as a destination for sending frames to.
  47. */
  48. typedef struct {
  49. BReactor *reactor;
  50. int frame_mtu;
  51. PacketPassFairQueue queue;
  52. PacketPassInactivityMonitor monitor;
  53. PacketPassNotifier notifier;
  54. DataProtoKeepaliveSource ka_source;
  55. PacketRecvBlocker ka_blocker;
  56. SinglePacketBuffer ka_buffer;
  57. PacketPassFairQueueFlow ka_qflow;
  58. BTimer receive_timer;
  59. int up;
  60. int up_report;
  61. DataProtoDest_handler handler;
  62. void *user;
  63. BPending keepalive_job;
  64. BPending up_job;
  65. int freeing;
  66. DebugCounter flows_counter;
  67. DebugObject d_obj;
  68. } DataProtoDest;
  69. /**
  70. * Object that receives frames from a device and routes
  71. * them to buffers in {@link DataProtoLocalSource} objects.
  72. */
  73. typedef struct {
  74. DataProtoDevice_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. } DataProtoDevice;
  84. /**
  85. * Local frame source.
  86. * Buffers frames received from the TAP device, addressed to a particular peer.
  87. */
  88. typedef struct {
  89. DataProtoDevice *device;
  90. peerid_t source_id;
  91. peerid_t dest_id;
  92. int inactivity_time;
  93. RouteBuffer rbuf;
  94. PacketPassInactivityMonitor monitor;
  95. PacketPassConnector connector;
  96. DataProtoDest *dp;
  97. PacketPassFairQueueFlow dp_qflow;
  98. DebugObject d_obj;
  99. } DataProtoLocalSource;
  100. /**
  101. * Initializes the object.
  102. *
  103. * @param o the object
  104. * @param reactor reactor we live in
  105. * @param output output interface. Must support cancel functionality. Its MTU must be
  106. * >=DATAPROTO_MAX_OVERHEAD.
  107. * @param keepalive_time keepalive time
  108. * @param tolerance_time after how long of not having received anything from the peer
  109. * to consider the link down
  110. * @param handler up state handler
  111. * @param user value to pass to handler
  112. * @return 1 on success, 0 on failure
  113. */
  114. int DataProtoDest_Init (DataProtoDest *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoDest_handler handler, void *user) WARN_UNUSED;
  115. /**
  116. * Frees the object.
  117. * There must be no local sources attached.
  118. *
  119. * @param o the object
  120. */
  121. void DataProtoDest_Free (DataProtoDest *o);
  122. /**
  123. * Prepares for freeing the object by allowing freeing of local sources.
  124. * The object enters freeing state.
  125. * The object must be freed before returning control to the reactor,
  126. * and before any further I/O (output or submitting frames).
  127. *
  128. * @param o the object
  129. */
  130. void DataProtoDest_PrepareFree (DataProtoDest *o);
  131. /**
  132. * Notifies the object that a packet was received from the peer.
  133. * Must not be in freeing state.
  134. *
  135. * @param o the object
  136. * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
  137. * Must be 0 or 1.
  138. */
  139. void DataProtoDest_Received (DataProtoDest *o, int peer_receiving);
  140. /**
  141. * Initiazes the object.
  142. *
  143. * @param o the object
  144. * @param input device input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
  145. * @param handler handler called when a packet arrives to allow the user to route it to
  146. * appropriate {@link DataProtoLocalSource} objects.
  147. * @param user value passed to handler
  148. * @param reactor reactor we live in
  149. * @return 1 on success, 0 on failure
  150. */
  151. int DataProtoDevice_Init (DataProtoDevice *o, PacketRecvInterface *input, DataProtoDevice_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  152. /**
  153. * Frees the object.
  154. * There must be no {@link DataProtoLocalSource} objects referring to this device.
  155. *
  156. * @param o the object
  157. */
  158. void DataProtoDevice_Free (DataProtoDevice *o);
  159. /**
  160. * Initializes the object.
  161. * The object is initialized in not attached state.
  162. *
  163. * @param o the object
  164. * @param device device to receive frames from
  165. * @param source_id source peer ID to encode in the headers (i.e. our ID)
  166. * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
  167. * object belongs to)
  168. * @param num_packets number of packets the buffer should hold. Must be >0.
  169. * @param inactivity_time milliseconds of output inactivity after which to call the
  170. * inactivity handler; <0 to disable. Note that the object is considered
  171. * active as long as its buffer is non-empty, even if is not attached to
  172. * a {@link DataProtoDest}.
  173. * @param handler_inactivity inactivity handler, if inactivity_time >=0
  174. * @param user value to pass to handler
  175. * @return 1 on success, 0 on failure
  176. */
  177. int DataProtoLocalSource_Init (
  178. DataProtoLocalSource *o, DataProtoDevice *device, peerid_t source_id, peerid_t dest_id, int num_packets,
  179. int inactivity_time, DataProtoLocalSource_handler_inactivity handler_inactivity, void *user
  180. ) WARN_UNUSED;
  181. /**
  182. * Frees the object.
  183. * The object must be in not attached state.
  184. *
  185. * @param o the object
  186. */
  187. void DataProtoLocalSource_Free (DataProtoLocalSource *o);
  188. /**
  189. * Routes a frame from the device to this object.
  190. * Must be called from within the job context of the {@link DataProtoDevice_handler} handler.
  191. * Must not be called after this has been called with more=0 for the current frame.
  192. *
  193. * @param o the object
  194. * @param more whether the current frame may have to be routed to more
  195. * objects. If 0, must not be called again until the handler is
  196. * called for the next frame. Must be 0 or 1.
  197. */
  198. void DataProtoLocalSource_Route (DataProtoLocalSource *o, int more);
  199. /**
  200. * Attaches the object to a destination.
  201. * The object must be in not attached state.
  202. *
  203. * @param o the object
  204. * @param dp destination to attach to. This object's frame_mtu must be <=
  205. * (output MTU of dp) - DATAPROTO_MAX_OVERHEAD.
  206. */
  207. void DataProtoLocalSource_Attach (DataProtoLocalSource *o, DataProtoDest *dp);
  208. /**
  209. * Detaches the object from a destination.
  210. * The object must be in attached state.
  211. *
  212. * @param o the object
  213. */
  214. void DataProtoLocalSource_Detach (DataProtoLocalSource *o);
  215. #endif