DatagramPeerIO.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * @file DatagramPeerIO.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Object for comminicating with a peer using a datagram socket.
  32. */
  33. #ifndef BADVPN_CLIENT_DATAGRAMPEERIO_H
  34. #define BADVPN_CLIENT_DATAGRAMPEERIO_H
  35. #include <stdint.h>
  36. #include <misc/debug.h>
  37. #include <protocol/spproto.h>
  38. #include <protocol/fragmentproto.h>
  39. #include <base/DebugObject.h>
  40. #include <base/BLog.h>
  41. #include <system/BReactor.h>
  42. #include <system/BAddr.h>
  43. #include <system/BDatagram.h>
  44. #include <system/BTime.h>
  45. #include <flow/PacketPassInterface.h>
  46. #include <flow/PacketPassConnector.h>
  47. #include <flow/SinglePacketBuffer.h>
  48. #include <flow/PacketRecvConnector.h>
  49. #include <flow/PacketPassNotifier.h>
  50. #include <client/FragmentProtoDisassembler.h>
  51. #include <client/FragmentProtoAssembler.h>
  52. #include <client/SPProtoEncoder.h>
  53. #include <client/SPProtoDecoder.h>
  54. /**
  55. * Callback function invoked when an error occurs with the peer connection.
  56. * The object has entered default state.
  57. * May be called from within a sending Send call.
  58. *
  59. * @param user as in {@link DatagramPeerIO_SetHandlers}
  60. */
  61. typedef void (*DatagramPeerIO_handler_error) (void *user);
  62. /**
  63. * Handler function invoked when the number of used OTPs has reached
  64. * the specified warning number in {@link DatagramPeerIO_SetOTPWarningHandler}.
  65. * May be called from within a sending Send call.
  66. *
  67. * @param user as in {@link DatagramPeerIO_SetHandlers}
  68. */
  69. typedef void (*DatagramPeerIO_handler_otp_warning) (void *user);
  70. /**
  71. * Handler called when OTP generation for a new receive seed is finished.
  72. *
  73. * @param user as in {@link DatagramPeerIO_SetHandlers}
  74. */
  75. typedef void (*DatagramPeerIO_handler_otp_ready) (void *user);
  76. /**
  77. * Object for comminicating with a peer using a datagram socket.
  78. *
  79. * The user provides data for sending to the peer through {@link PacketPassInterface}.
  80. * Received data is provided to the user through {@link PacketPassInterface}.
  81. *
  82. * The object has a logical state called a mode, which is one of the following:
  83. * - default - nothing is send or received
  84. * - connecting - an address was provided by the user for sending datagrams to.
  85. * Datagrams are being sent to that address through a socket,
  86. * and datagrams are being received on the same socket.
  87. * - binding - an address was provided by the user to bind a socket to.
  88. * Datagrams are being received on the socket. Datagrams are not being
  89. * sent initially. When a datagram is received, its source address is
  90. * used as a destination address for sending datagrams.
  91. */
  92. typedef struct {
  93. DebugObject d_obj;
  94. BReactor *reactor;
  95. int payload_mtu;
  96. struct spproto_security_params sp_params;
  97. void *user;
  98. BLog_logfunc logfunc;
  99. DatagramPeerIO_handler_error handler_error;
  100. int spproto_payload_mtu;
  101. int effective_socket_mtu;
  102. // sending base
  103. FragmentProtoDisassembler send_disassembler;
  104. SPProtoEncoder send_encoder;
  105. SinglePacketBuffer send_buffer;
  106. PacketPassConnector send_connector;
  107. // receiving
  108. PacketRecvConnector recv_connector;
  109. SinglePacketBuffer recv_buffer;
  110. SPProtoDecoder recv_decoder;
  111. PacketPassNotifier recv_notifier;
  112. FragmentProtoAssembler recv_assembler;
  113. // mode
  114. int mode;
  115. // datagram object
  116. BDatagram dgram;
  117. } DatagramPeerIO;
  118. /**
  119. * Initializes the object.
  120. * The interface is initialized in default mode.
  121. * {@link BLog_Init} must have been done.
  122. * {@link BNetwork_GlobalInit} must have been done.
  123. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  124. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  125. *
  126. * @param o the object
  127. * @param reactor {@link BReactor} we live in
  128. * @param payload_mtu maximum payload size. Must be >=0.
  129. * @param socket_mtu maximum datagram size for the socket. Must be >=0. Must be large enough so it is possible to
  130. * send a FragmentProto chunk with one byte of data over SPProto, i.e. the following has to hold:
  131. * spproto_payload_mtu_for_carrier_mtu(sp_params, socket_mtu) > sizeof(struct fragmentproto_chunk_header)
  132. * @param sp_params SPProto security parameters
  133. * @param latency latency parameter to {@link FragmentProtoDisassembler_Init}.
  134. * @param num_frames num_frames parameter to {@link FragmentProtoAssembler_Init}. Must be >0.
  135. * @param recv_userif interface to pass received packets to the user. Its MTU must be >=payload_mtu.
  136. * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
  137. * In this case, must be >0 and <=sp_params.otp_num.
  138. * @param twd thread work dispatcher
  139. * @param user value to pass to handlers
  140. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  141. * @param handler_error error handler
  142. * @param handler_otp_warning OTP warning handler
  143. * @param handler_otp_ready handler called when OTP generation for a new receive seed is finished
  144. * @return 1 on success, 0 on failure
  145. */
  146. int DatagramPeerIO_Init (
  147. DatagramPeerIO *o,
  148. BReactor *reactor,
  149. int payload_mtu,
  150. int socket_mtu,
  151. struct spproto_security_params sp_params,
  152. btime_t latency,
  153. int num_frames,
  154. PacketPassInterface *recv_userif,
  155. int otp_warning_count,
  156. BThreadWorkDispatcher *twd,
  157. void *user,
  158. BLog_logfunc logfunc,
  159. DatagramPeerIO_handler_error handler_error,
  160. DatagramPeerIO_handler_otp_warning handler_otp_warning,
  161. DatagramPeerIO_handler_otp_ready handler_otp_ready
  162. ) WARN_UNUSED;
  163. /**
  164. * Frees the object.
  165. *
  166. * @param o the object
  167. */
  168. void DatagramPeerIO_Free (DatagramPeerIO *o);
  169. /**
  170. * Returns an interface the user should use to send packets.
  171. * The OTP warning handler may be called from within Send calls
  172. * to the interface.
  173. *
  174. * @param o the object
  175. * @return sending interface
  176. */
  177. PacketPassInterface * DatagramPeerIO_GetSendInput (DatagramPeerIO *o);
  178. /**
  179. * Attempts to establish connection to the peer which has bound to an address.
  180. * On success, the interface enters connecting mode.
  181. * On failure, the interface enters default mode.
  182. *
  183. * @param o the object
  184. * @param addr address to send packets to
  185. * @return 1 on success, 0 on failure
  186. */
  187. int DatagramPeerIO_Connect (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
  188. /**
  189. * Attempts to establish connection to the peer by binding to an address.
  190. * On success, the interface enters connecting mode.
  191. * On failure, the interface enters default mode.
  192. *
  193. * @param o the object
  194. * @param addr address to bind to. Must be supported according to
  195. * {@link BDatagram_AddressFamilySupported}.
  196. * @return 1 on success, 0 on failure
  197. */
  198. int DatagramPeerIO_Bind (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
  199. /**
  200. * Sets the encryption key to use for sending and receiving.
  201. * Encryption must be enabled.
  202. *
  203. * @param o the object
  204. * @param encryption_key key to use
  205. */
  206. void DatagramPeerIO_SetEncryptionKey (DatagramPeerIO *o, uint8_t *encryption_key);
  207. /**
  208. * Removed the encryption key to use for sending and receiving.
  209. * Encryption must be enabled.
  210. *
  211. * @param o the object
  212. */
  213. void DatagramPeerIO_RemoveEncryptionKey (DatagramPeerIO *o);
  214. /**
  215. * Sets the OTP seed for sending.
  216. * OTPs must be enabled.
  217. *
  218. * @param o the object
  219. * @param seed_id seed identifier
  220. * @param key OTP encryption key
  221. * @param iv OTP initialization vector
  222. */
  223. void DatagramPeerIO_SetOTPSendSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  224. /**
  225. * Removes the OTP seed for sending of one is configured.
  226. * OTPs must be enabled.
  227. *
  228. * @param o the object
  229. */
  230. void DatagramPeerIO_RemoveOTPSendSeed (DatagramPeerIO *o);
  231. /**
  232. * Adds an OTP seed for reciving.
  233. * OTPs must be enabled.
  234. *
  235. * @param o the object
  236. * @param seed_id seed identifier
  237. * @param key OTP encryption key
  238. * @param iv OTP initialization vector
  239. */
  240. void DatagramPeerIO_AddOTPRecvSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  241. /**
  242. * Removes all OTP seeds for reciving.
  243. * OTPs must be enabled.
  244. *
  245. * @param o the object
  246. */
  247. void DatagramPeerIO_RemoveOTPRecvSeeds (DatagramPeerIO *o);
  248. #endif