DataProto.h 7.8 KB

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