DatagramPeerIO.h 7.9 KB

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