DatagramPeerIO.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * @file DatagramPeerIO.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. * Object for comminicating with a peer using a datagram socket.
  25. */
  26. #ifndef BADVPN_CLIENT_DATAGRAMPEERIO_H
  27. #define BADVPN_CLIENT_DATAGRAMPEERIO_H
  28. #include <stdint.h>
  29. #include <misc/debug.h>
  30. #include <protocol/spproto.h>
  31. #include <protocol/fragmentproto.h>
  32. #include <base/DebugObject.h>
  33. #include <system/BReactor.h>
  34. #include <system/BAddr.h>
  35. #include <system/BDatagram.h>
  36. #include <system/BTime.h>
  37. #include <flow/PacketPassInterface.h>
  38. #include <flow/PacketPassConnector.h>
  39. #include <flow/SinglePacketBuffer.h>
  40. #include <flow/PacketRecvConnector.h>
  41. #include <flow/PacketPassNotifier.h>
  42. #include <client/FragmentProtoDisassembler.h>
  43. #include <client/FragmentProtoAssembler.h>
  44. #include <client/SPProtoEncoder.h>
  45. #include <client/SPProtoDecoder.h>
  46. typedef void (*DatagramPeerIO_logfunc) (void *user);
  47. /**
  48. * Callback function invoked when an error occurs with the peer connection.
  49. * The object has entered default state.
  50. * May be called from within a sending Send call.
  51. *
  52. * @param user as in {@link DatagramPeerIO_SetHandlers}
  53. */
  54. typedef void (*DatagramPeerIO_handler_error) (void *user);
  55. /**
  56. * Handler function invoked when the number of used OTPs has reached
  57. * the specified warning number in {@link DatagramPeerIO_SetOTPWarningHandler}.
  58. * May be called from within a sending Send call.
  59. *
  60. * @param user as in {@link DatagramPeerIO_SetHandlers}
  61. */
  62. typedef void (*DatagramPeerIO_handler_otp_warning) (void *user);
  63. /**
  64. * Handler called when OTP generation for a new receive seed is finished.
  65. *
  66. * @param user as in {@link DatagramPeerIO_SetHandlers}
  67. */
  68. typedef void (*DatagramPeerIO_handler_otp_ready) (void *user);
  69. /**
  70. * Object for comminicating with a peer using a datagram socket.
  71. *
  72. * The user provides data for sending to the peer through {@link PacketPassInterface}.
  73. * Received data is provided to the user through {@link PacketPassInterface}.
  74. *
  75. * The object has a logical state called a mode, which is one of the following:
  76. * - default - nothing is send or received
  77. * - connecting - an address was provided by the user for sending datagrams to.
  78. * Datagrams are being sent to that address through a socket,
  79. * and datagrams are being received on the same socket.
  80. * - binding - an address was provided by the user to bind a socket to.
  81. * Datagrams are being received on the socket. Datagrams are not being
  82. * sent initially. When a datagram is received, its source address is
  83. * used as a destination address for sending datagrams.
  84. */
  85. typedef struct {
  86. DebugObject d_obj;
  87. BReactor *reactor;
  88. int payload_mtu;
  89. struct spproto_security_params sp_params;
  90. void *user;
  91. DatagramPeerIO_logfunc logfunc;
  92. DatagramPeerIO_handler_error handler_error;
  93. int spproto_payload_mtu;
  94. int effective_socket_mtu;
  95. // sending base
  96. FragmentProtoDisassembler send_disassembler;
  97. SPProtoEncoder send_encoder;
  98. SinglePacketBuffer send_buffer;
  99. PacketPassConnector send_connector;
  100. // receiving
  101. PacketRecvConnector recv_connector;
  102. SinglePacketBuffer recv_buffer;
  103. SPProtoDecoder recv_decoder;
  104. PacketPassNotifier recv_notifier;
  105. FragmentProtoAssembler recv_assembler;
  106. // mode
  107. int mode;
  108. // datagram object
  109. BDatagram dgram;
  110. } DatagramPeerIO;
  111. /**
  112. * Initializes the object.
  113. * The interface is initialized in default mode.
  114. * {@link BLog_Init} must have been done.
  115. * {@link BNetwork_GlobalInit} must have been done.
  116. * {@link BSecurity_GlobalInitThreadSafe} must have been done if
  117. * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
  118. *
  119. * @param o the object
  120. * @param reactor {@link BReactor} we live in
  121. * @param payload_mtu maximum payload size. Must be >=0.
  122. * @param socket_mtu maximum datagram size for the socket. Must be >=0. Must be large enough so it is possible to
  123. * send a FragmentProto chunk with one byte of data over SPProto, i.e. the following has to hold:
  124. * spproto_payload_mtu_for_carrier_mtu(sp_params, socket_mtu) > sizeof(struct fragmentproto_chunk_header)
  125. * @param sp_params SPProto security parameters
  126. * @param latency latency parameter to {@link FragmentProtoDisassembler_Init}.
  127. * @param num_frames num_frames parameter to {@link FragmentProtoAssembler_Init}. Must be >0.
  128. * @param recv_userif interface to pass received packets to the user. Its MTU must be >=payload_mtu.
  129. * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
  130. * In this case, must be >0 and <=sp_params.otp_num.
  131. * @param twd thread work dispatcher
  132. * @param user value to pass to handlers
  133. * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  134. * @param handler_error error handler
  135. * @param handler_otp_warning OTP warning handler
  136. * @param handler_otp_ready handler called when OTP generation for a new receive seed is finished
  137. * @return 1 on success, 0 on failure
  138. */
  139. int DatagramPeerIO_Init (
  140. DatagramPeerIO *o,
  141. BReactor *reactor,
  142. int payload_mtu,
  143. int socket_mtu,
  144. struct spproto_security_params sp_params,
  145. btime_t latency,
  146. int num_frames,
  147. PacketPassInterface *recv_userif,
  148. int otp_warning_count,
  149. BThreadWorkDispatcher *twd,
  150. void *user,
  151. DatagramPeerIO_logfunc logfunc,
  152. DatagramPeerIO_handler_error handler_error,
  153. DatagramPeerIO_handler_otp_warning handler_otp_warning,
  154. DatagramPeerIO_handler_otp_ready handler_otp_ready
  155. ) WARN_UNUSED;
  156. /**
  157. * Frees the object.
  158. *
  159. * @param o the object
  160. */
  161. void DatagramPeerIO_Free (DatagramPeerIO *o);
  162. /**
  163. * Returns an interface the user should use to send packets.
  164. * The OTP warning handler may be called from within Send calls
  165. * to the interface.
  166. *
  167. * @param o the object
  168. * @return sending interface
  169. */
  170. PacketPassInterface * DatagramPeerIO_GetSendInput (DatagramPeerIO *o);
  171. /**
  172. * Attempts to establish connection to the peer which has bound to an address.
  173. * On success, the interface enters connecting mode.
  174. * On failure, the interface enters default mode.
  175. *
  176. * @param o the object
  177. * @param addr address to send packets to
  178. * @return 1 on success, 0 on failure
  179. */
  180. int DatagramPeerIO_Connect (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
  181. /**
  182. * Attempts to establish connection to the peer by binding to an address.
  183. * On success, the interface enters connecting mode.
  184. * On failure, the interface enters default mode.
  185. *
  186. * @param o the object
  187. * @param addr address to bind to. Must be supported according to
  188. * {@link BDatagram_AddressFamilySupported}.
  189. * @return 1 on success, 0 on failure
  190. */
  191. int DatagramPeerIO_Bind (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
  192. /**
  193. * Sets the encryption key to use for sending and receiving.
  194. * Encryption must be enabled.
  195. *
  196. * @param o the object
  197. * @param encryption_key key to use
  198. */
  199. void DatagramPeerIO_SetEncryptionKey (DatagramPeerIO *o, uint8_t *encryption_key);
  200. /**
  201. * Removed the encryption key to use for sending and receiving.
  202. * Encryption must be enabled.
  203. *
  204. * @param o the object
  205. */
  206. void DatagramPeerIO_RemoveEncryptionKey (DatagramPeerIO *o);
  207. /**
  208. * Sets the OTP seed for sending.
  209. * OTPs must be enabled.
  210. *
  211. * @param o the object
  212. * @param seed_id seed identifier
  213. * @param key OTP encryption key
  214. * @param iv OTP initialization vector
  215. */
  216. void DatagramPeerIO_SetOTPSendSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  217. /**
  218. * Removes the OTP seed for sending of one is configured.
  219. * OTPs must be enabled.
  220. *
  221. * @param o the object
  222. */
  223. void DatagramPeerIO_RemoveOTPSendSeed (DatagramPeerIO *o);
  224. /**
  225. * Adds an OTP seed for reciving.
  226. * OTPs must be enabled.
  227. *
  228. * @param o the object
  229. * @param seed_id seed identifier
  230. * @param key OTP encryption key
  231. * @param iv OTP initialization vector
  232. */
  233. void DatagramPeerIO_AddOTPRecvSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  234. /**
  235. * Removes all OTP seeds for reciving.
  236. * OTPs must be enabled.
  237. *
  238. * @param o the object
  239. */
  240. void DatagramPeerIO_RemoveOTPRecvSeeds (DatagramPeerIO *o);
  241. #endif