DataProto.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /**
  45. * Frame destination.
  46. * Represents a peer as a destination for sending frames to.
  47. */
  48. typedef struct {
  49. BReactor *reactor;
  50. int mtu;
  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. #ifndef NDEBUG
  68. PacketPassInterface *d_output;
  69. #endif
  70. } DataProtoDest;
  71. /**
  72. * Object that receives frames from a device and routes
  73. * them to buffers in {@link DataProtoLocalSource} objects.
  74. */
  75. typedef struct {
  76. DataProtoDevice_handler handler;
  77. void *user;
  78. BReactor *reactor;
  79. int frame_mtu;
  80. PacketRouter router;
  81. uint8_t *current_buf;
  82. int current_recv_len;
  83. DebugObject d_obj;
  84. DebugCounter d_ctr;
  85. } DataProtoDevice;
  86. /**
  87. * Local frame source.
  88. * Buffers frames received from the TAP device, addressed to a particular peer.
  89. */
  90. typedef struct {
  91. DataProtoDevice *device;
  92. peerid_t source_id;
  93. peerid_t dest_id;
  94. RouteBuffer rbuf;
  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. * >=sizeof(struct dataproto_header)+sizeof(struct dataproto_peer_id).
  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. * Must not be called from output Send calls.
  135. * May call the up state handler.
  136. * May invoke output I/O.
  137. *
  138. * @param o the object
  139. * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
  140. * Must be 0 or 1.
  141. */
  142. void DataProtoDest_Received (DataProtoDest *o, int peer_receiving);
  143. /**
  144. * Initiazes the object.
  145. *
  146. * @param o the object
  147. * @param input device input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
  148. * @param handler handler called when a packet arrives to allow the user to route it to
  149. * appropriate {@link DataProtoLocalSource} objects.
  150. * @param user value passed to handler
  151. * @param reactor reactor we live in
  152. * @return 1 on success, 0 on failure
  153. */
  154. int DataProtoDevice_Init (DataProtoDevice *o, PacketRecvInterface *input, DataProtoDevice_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  155. /**
  156. * Frees the object.
  157. * There must be no {@link DataProtoLocalSource} objects referring to this device.
  158. *
  159. * @param o the object
  160. */
  161. void DataProtoDevice_Free (DataProtoDevice *o);
  162. /**
  163. * Initializes the object.
  164. * The object is initialized in not attached state.
  165. *
  166. * @param o the object
  167. * @param device device to receive frames from
  168. * @param source_id source peer ID to encode in the headers (i.e. our ID)
  169. * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
  170. * object belongs to)
  171. * @param num_packets number of packets the buffer should hold. Must be >0.
  172. * @return 1 on success, 0 on failure
  173. */
  174. int DataProtoLocalSource_Init (DataProtoLocalSource *o, DataProtoDevice *device, peerid_t source_id, peerid_t dest_id, int num_packets) WARN_UNUSED;
  175. /**
  176. * Frees the object.
  177. * The object must be in not attached state.
  178. *
  179. * @param o the object
  180. */
  181. void DataProtoLocalSource_Free (DataProtoLocalSource *o);
  182. /**
  183. * Routes a frame from the device to this object.
  184. * Must be called from within the job context of the {@link DataProtoDevice_handler} handler.
  185. * Must not be called after this has been called with more=0 for the current frame.
  186. *
  187. * @param o the object
  188. * @param more whether the current frame may have to be routed to more
  189. * objects. If 0, must not be called again until the handler is
  190. * called for the next frame. Must be 0 or 1.
  191. */
  192. void DataProtoLocalSource_Route (DataProtoLocalSource *o, int more);
  193. /**
  194. * Attaches the object to a destination.
  195. * The object must be in not attached state.
  196. *
  197. * @param o the object
  198. * @param dp destination to attach to. This object's frame_mtu must be <= destination's
  199. * (output MTU)-(sizeof(struct dataproto_header)+sizeof(struct dataproto_peer_id)).
  200. */
  201. void DataProtoLocalSource_Attach (DataProtoLocalSource *o, DataProtoDest *dp);
  202. /**
  203. * Detaches the object from a destination.
  204. * The object must be in attached state.
  205. * Unless the destination is in freeing state, must not be called from destination's
  206. * output Send calls.
  207. *
  208. * @param o the object
  209. */
  210. void DataProtoLocalSource_Detach (DataProtoLocalSource *o);
  211. #endif