DataProto.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 (*DataProtoSink_handler) (void *user, int up);
  42. typedef void (*DataProtoSource_handler) (void *user, const uint8_t *frame, int frame_len);
  43. typedef void (*DataProtoFlow_handler_inactivity) (void *user);
  44. struct DataProtoFlow_buffer;
  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. int up_report;
  62. DataProtoSink_handler handler;
  63. void *user;
  64. BPending up_job;
  65. struct DataProtoFlow_buffer *detaching_buffer;
  66. DebugObject d_obj;
  67. DebugCounter d_ctr;
  68. } DataProtoSink;
  69. /**
  70. * Object that receives frames from a device and routes
  71. * them to buffers in {@link DataProtoFlow} objects.
  72. */
  73. typedef struct {
  74. DataProtoSource_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. } DataProtoSource;
  84. /**
  85. * Local frame source.
  86. * Buffers frames received from the TAP device, addressed to a particular peer.
  87. */
  88. typedef struct {
  89. DataProtoSource *device;
  90. peerid_t source_id;
  91. peerid_t dest_id;
  92. DataProtoSink *dp_desired;
  93. struct DataProtoFlow_buffer *b;
  94. DebugObject d_obj;
  95. } DataProtoFlow;
  96. struct DataProtoFlow_buffer {
  97. DataProtoFlow *flow;
  98. int inactivity_time;
  99. RouteBuffer rbuf;
  100. PacketPassInactivityMonitor monitor;
  101. PacketPassConnector connector;
  102. DataProtoSink *dp;
  103. PacketPassFairQueueFlow dp_qflow;
  104. };
  105. /**
  106. * Initializes the object.
  107. *
  108. * @param o the object
  109. * @param reactor reactor we live in
  110. * @param output output interface. Must support cancel functionality. Its MTU must be
  111. * >=DATAPROTO_MAX_OVERHEAD.
  112. * @param keepalive_time keepalive time
  113. * @param tolerance_time after how long of not having received anything from the peer
  114. * to consider the link down
  115. * @param handler up state handler
  116. * @param user value to pass to handler
  117. * @return 1 on success, 0 on failure
  118. */
  119. int DataProtoSink_Init (DataProtoSink *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoSink_handler handler, void *user) WARN_UNUSED;
  120. /**
  121. * Frees the object.
  122. * There must be no local sources attached.
  123. *
  124. * @param o the object
  125. */
  126. void DataProtoSink_Free (DataProtoSink *o);
  127. /**
  128. * Notifies the object that a packet was received from the peer.
  129. * Must not be in freeing state.
  130. *
  131. * @param o the object
  132. * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
  133. * Must be 0 or 1.
  134. */
  135. void DataProtoSink_Received (DataProtoSink *o, int peer_receiving);
  136. /**
  137. * Initiazes the object.
  138. *
  139. * @param o the object
  140. * @param input device input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
  141. * @param handler handler called when a packet arrives to allow the user to route it to
  142. * appropriate {@link DataProtoFlow} objects.
  143. * @param user value passed to handler
  144. * @param reactor reactor we live in
  145. * @return 1 on success, 0 on failure
  146. */
  147. int DataProtoSource_Init (DataProtoSource *o, PacketRecvInterface *input, DataProtoSource_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  148. /**
  149. * Frees the object.
  150. * There must be no {@link DataProtoFlow} objects referring to this device.
  151. *
  152. * @param o the object
  153. */
  154. void DataProtoSource_Free (DataProtoSource *o);
  155. /**
  156. * Initializes the object.
  157. * The object is initialized in not attached state.
  158. *
  159. * @param o the object
  160. * @param device device to receive frames from
  161. * @param source_id source peer ID to encode in the headers (i.e. our ID)
  162. * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
  163. * object belongs to)
  164. * @param num_packets number of packets the buffer should hold. Must be >0.
  165. * @param inactivity_time milliseconds of output inactivity after which to call the
  166. * inactivity handler; <0 to disable. Note that the object is considered
  167. * active as long as its buffer is non-empty, even if is not attached to
  168. * a {@link DataProtoSink}.
  169. * @param handler_inactivity inactivity handler, if inactivity_time >=0
  170. * @param user value to pass to handler
  171. * @return 1 on success, 0 on failure
  172. */
  173. int DataProtoFlow_Init (
  174. DataProtoFlow *o, DataProtoSource *device, peerid_t source_id, peerid_t dest_id, int num_packets,
  175. int inactivity_time, DataProtoFlow_handler_inactivity handler_inactivity, void *user
  176. ) WARN_UNUSED;
  177. /**
  178. * Frees the object.
  179. * The object must be in not attached state.
  180. *
  181. * @param o the object
  182. */
  183. void DataProtoFlow_Free (DataProtoFlow *o);
  184. /**
  185. * Routes a frame from the device to this object.
  186. * Must be called from within the job context of the {@link DataProtoSource_handler} handler.
  187. * Must not be called after this has been called with more=0 for the current frame.
  188. *
  189. * @param o the object
  190. * @param more whether the current frame may have to be routed to more
  191. * objects. If 0, must not be called again until the handler is
  192. * called for the next frame. Must be 0 or 1.
  193. */
  194. void DataProtoFlow_Route (DataProtoFlow *o, int more);
  195. /**
  196. * Attaches the object to a destination.
  197. * The object must be in not attached state.
  198. *
  199. * @param o the object
  200. * @param dp destination to attach to. This object's frame_mtu must be <=
  201. * (output MTU of dp) - DATAPROTO_MAX_OVERHEAD.
  202. */
  203. void DataProtoFlow_Attach (DataProtoFlow *o, DataProtoSink *dp);
  204. /**
  205. * Detaches the object from a destination.
  206. * The object must be in attached state.
  207. *
  208. * @param o the object
  209. */
  210. void DataProtoFlow_Detach (DataProtoFlow *o);
  211. #endif