DatagramPeerIO.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/dead.h>
  30. #include <misc/debug.h>
  31. #include <protocol/spproto.h>
  32. #include <protocol/fragmentproto.h>
  33. #include <system/DebugObject.h>
  34. #include <system/BReactor.h>
  35. #include <system/BAddr.h>
  36. #include <system/BSocket.h>
  37. #include <system/BTime.h>
  38. #include <flow/PacketPassInterface.h>
  39. #include <flow/DatagramSocketSink.h>
  40. #include <flow/PacketPassConnector.h>
  41. #include <flow/SinglePacketBuffer.h>
  42. #include <flow/SPProtoEncoder.h>
  43. #include <flow/FragmentProtoDisassembler.h>
  44. #include <flow/DatagramSocketSource.h>
  45. #include <flow/PacketRecvConnector.h>
  46. #include <flow/SPProtoDecoder.h>
  47. #include <flow/FragmentProtoAssembler.h>
  48. #include <flow/PacketPassNotifier.h>
  49. #include <flow/PacketRecvNotifier.h>
  50. /**
  51. * Handler function invoked when the number of used OTPs has reached
  52. * the specified warning number in {@link DatagramPeerIO_SetOTPWarningHandler}.
  53. * May be called from within a sending Send call.
  54. *
  55. * @param user as in {@link DatagramPeerIO_SetOTPWarningHandler}
  56. */
  57. typedef void (*DatagramPeerIO_handler_otp_warning) (void *user);
  58. /**
  59. * Object for comminicating with a peer using a datagram socket.
  60. *
  61. * The user provides data for sending to the peer through {@link PacketPassInterface}.
  62. * Received data is provided to the user through {@link PacketPassInterface}.
  63. *
  64. * The object has a logical state called a mode, which is one of the following:
  65. * - default - nothing is send or received
  66. * - connecting - an address was provided by the user for sending datagrams to.
  67. * Datagrams are being sent to that address through a socket,
  68. * and datagrams are being received on the same socket.
  69. * - binding - an address was provided by the user to bind a socket to.
  70. * Datagrams are being received on the socket. Datagrams are not being
  71. * sent initially. When a datagram is received, its source address is
  72. * used as a destination address for sending datagrams.
  73. */
  74. typedef struct {
  75. DebugObject d_obj;
  76. dead_t dead;
  77. BReactor *reactor;
  78. int payload_mtu;
  79. struct spproto_security_params sp_params;
  80. int spproto_payload_mtu;
  81. int effective_socket_mtu;
  82. // flow error domain
  83. FlowErrorDomain domain;
  84. // encoder group
  85. SPProtoEncoderGroup encoder_group;
  86. // persistent I/O objects
  87. // sending base
  88. FragmentProtoDisassembler send_disassembler;
  89. SPProtoEncoder send_encoder;
  90. PacketRecvNotifier send_notifier;
  91. SinglePacketBuffer send_buffer;
  92. PacketPassConnector send_connector;
  93. // receiving
  94. PacketRecvConnector recv_connector;
  95. SinglePacketBuffer recv_buffer;
  96. SPProtoDecoder recv_decoder;
  97. PacketPassNotifier recv_notifier;
  98. FragmentProtoAssembler recv_assembler;
  99. // OTP warning handler
  100. DatagramPeerIO_handler_otp_warning handler_otp_warning;
  101. void *handler_otp_warning_user;
  102. int handler_otp_warning_num_used;
  103. // mode
  104. int mode;
  105. dead_t mode_dead;
  106. // in binded mode, whether sending is up
  107. int bind_sending_up;
  108. // datagram socket
  109. BSocket sock;
  110. // non-persistent sending objects
  111. DatagramSocketSink send_sink;
  112. // non-persistent receiving objects
  113. DatagramSocketSource recv_source;
  114. } DatagramPeerIO;
  115. /**
  116. * Initializes the object.
  117. * The interface is initialized in default mode.
  118. * {@link BLog_Init} must have been done.
  119. *
  120. * @param o the object
  121. * @param reactor {@link BReactor} we live in
  122. * @param payload_mtu maximum payload size. Must be >=0.
  123. * @param socket_mtu maximum datagram size for the socket. Must be >=0. Must be large enough so it is possible to
  124. * send a FragmentProto chunk with one byte of data over SPProto, i.e. the following has to hold:
  125. * spproto_payload_mtu_for_carrier_mtu(sp_params, socket_mtu) > sizeof(struct fragmentproto_chunk_header)
  126. * @param sp_params SPProto security parameters. Must be valid according to {@link spproto_validate_security_params}.
  127. * @param latency latency parameter to {@link FragmentProtoDisassembler_Init}.
  128. * @param recv_userif interface to pass received packets to the user. Its MTU must be >=payload_mtu.
  129. * @return 1 on success, 0 on failure
  130. */
  131. int DatagramPeerIO_Init (DatagramPeerIO *o, BReactor *reactor, int payload_mtu, int socket_mtu, struct spproto_security_params sp_params, btime_t latency, PacketPassInterface *recv_userif) WARN_UNUSED;
  132. /**
  133. * Frees the object.
  134. *
  135. * @param o the object
  136. */
  137. void DatagramPeerIO_Free (DatagramPeerIO *o);
  138. /**
  139. * Returns an interface the user should use to send packets.
  140. * The OTP warning handler may be called from within Send calls
  141. * to the interface.
  142. *
  143. * @param o the object
  144. * @return sending interface
  145. */
  146. PacketPassInterface * DatagramPeerIO_GetSendInput (DatagramPeerIO *o);
  147. /**
  148. * Breaks down the connection if one is configured.
  149. * The interface enters default mode.
  150. *
  151. * @param o the object
  152. */
  153. void DatagramPeerIO_Disconnect (DatagramPeerIO *o);
  154. /**
  155. * Attempts to establish connection to the peer which has bound to an address.
  156. * On success, the interface enters connecting mode.
  157. * On failure, the interface enters default mode.
  158. *
  159. * @param o the object
  160. * @param addr address to send packets to. Must be recognized and not invalid.
  161. * @return 1 on success, 0 on failure
  162. */
  163. int DatagramPeerIO_Connect (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
  164. /**
  165. * Attempts to establish connection to the peer by binding to an address.
  166. * On success, the interface enters connecting mode.
  167. * On failure, the interface enters default mode.
  168. *
  169. * @param o the object
  170. * @param addr address to bind to. Must be recognized and not invalid.
  171. * @return 1 on success, 0 on failure
  172. */
  173. int DatagramPeerIO_Bind (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
  174. /**
  175. * Removes any internally buffered packets for sending.
  176. * This can be used when configuring a new connecion to prevent packets encoded with
  177. * previous parameters from being sent over the new connection.
  178. *
  179. * @param o the object
  180. */
  181. void DatagramPeerIO_Flush (DatagramPeerIO *o);
  182. /**
  183. * Sets the encryption key to use for sending and receiving.
  184. * Encryption must be enabled.
  185. *
  186. * @param o the object
  187. * @param encryption_key key to use
  188. */
  189. void DatagramPeerIO_SetEncryptionKey (DatagramPeerIO *o, uint8_t *encryption_key);
  190. /**
  191. * Removed the encryption key to use for sending and receiving.
  192. * Encryption must be enabled.
  193. *
  194. * @param o the object
  195. */
  196. void DatagramPeerIO_RemoveEncryptionKey (DatagramPeerIO *o);
  197. /**
  198. * Sets the OTP seed for sending.
  199. * OTPs must be enabled.
  200. *
  201. * @param o the object
  202. * @param seed_id seed identifier
  203. * @param key OTP encryption key
  204. * @param iv OTP initialization vector
  205. */
  206. void DatagramPeerIO_SetOTPSendSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  207. /**
  208. * Removes the OTP seed for sending of one is configured.
  209. * OTPs must be enabled.
  210. *
  211. * @param o the object
  212. */
  213. void DatagramPeerIO_RemoveOTPSendSeed (DatagramPeerIO *o);
  214. /**
  215. * Adds an OTP seed for reciving.
  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_AddOTPRecvSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
  224. /**
  225. * Removes all OTP seeds for reciving.
  226. * OTPs must be enabled.
  227. *
  228. * @param o the object
  229. */
  230. void DatagramPeerIO_RemoveOTPRecvSeeds (DatagramPeerIO *o);
  231. /**
  232. * Sets the OTP warning handler.
  233. * OTPs must be enabled.
  234. *
  235. * @param o the object
  236. * @param handler handler function. NULL to disable handler.
  237. * @param user value passed to handler function
  238. * @param num_used after how many used OTPs to invoke the handler. Must be >0 unless handler is NULL.
  239. * The handler will be invoked when exactly that many OTPs have been used. If the handler
  240. * is configured when the warning level has already been reached, it will not be called
  241. * until a new send seed is set or the handler is reconfigured.
  242. */
  243. void DatagramPeerIO_SetOTPWarningHandler (DatagramPeerIO *o, DatagramPeerIO_handler_otp_warning handler, void *user, int num_used);
  244. #endif