BSocksClient.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @file BSocksClient.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. * SOCKS5 client. TCP only, no authentication.
  32. */
  33. #ifndef BADVPN_SOCKS_BSOCKSCLIENT_H
  34. #define BADVPN_SOCKS_BSOCKSCLIENT_H
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <stdbool.h>
  38. #include <misc/debug.h>
  39. #include <misc/debugerror.h>
  40. #include <misc/socks_proto.h>
  41. #include <misc/packed.h>
  42. #include <base/DebugObject.h>
  43. #include <base/BPending.h>
  44. #include <system/BConnection.h>
  45. #include <flow/PacketStreamSender.h>
  46. #define BSOCKSCLIENT_EVENT_ERROR 1
  47. #define BSOCKSCLIENT_EVENT_UP 2
  48. #define BSOCKSCLIENT_EVENT_ERROR_CLOSED 3
  49. #define BSOCKSCLIENT_EVENT_CONNECTED 4
  50. /**
  51. * Handler for events generated by the SOCKS client.
  52. *
  53. * The event is one of the following:
  54. * - BSOCKSCLIENT_EVENT_ERROR: An error has occured. The object must be freed from the
  55. * job closure of the handler and no further I/O must be attempted.
  56. * - BSOCKSCLIENT_EVENT_ERROR_CLOSED: The server has closed the connection. This event
  57. * can only be reported after BSOCKSCLIENT_EVENT_UP. The object must be freed from
  58. * the job closure of the handler and no further I/O must be attempted.
  59. * - BSOCKSCLIENT_EVENT_UP: The CONNECT or UDP ASSOCIATE operation was successful. In
  60. * the case of CONNECT, application I/O may now begin.
  61. * - BSOCKSCLIENT_EVENT_CONNECTED: The TCP connection to the server has been established
  62. * and the SOCKS protocol is about to begin. The local address of the TCP connection is
  63. * now available via @ref BSocksClient_GetLocalAddr. The job closure of this callback
  64. * is the last chance to call @ref BSocksClient_SetDestAddr.
  65. *
  66. * @param user as in {@link BSocksClient_Init}
  67. * @param event See above.
  68. */
  69. typedef void (*BSocksClient_handler) (void *user, int event);
  70. struct BSocksClient_auth_info {
  71. int auth_type;
  72. union {
  73. struct {
  74. const char *username;
  75. size_t username_len;
  76. const char *password;
  77. size_t password_len;
  78. } password;
  79. };
  80. };
  81. typedef struct {
  82. const struct BSocksClient_auth_info *auth_info;
  83. size_t num_auth_info;
  84. BAddr dest_addr;
  85. bool udp;
  86. BAddr bind_addr;
  87. BSocksClient_handler handler;
  88. void *user;
  89. BReactor *reactor;
  90. int state;
  91. char *buffer;
  92. BConnector connector;
  93. BConnection con;
  94. BPending continue_job;
  95. union {
  96. struct {
  97. PacketPassInterface *send_if;
  98. PacketStreamSender send_sender;
  99. StreamRecvInterface *recv_if;
  100. uint8_t *recv_dest;
  101. int recv_len;
  102. int recv_total;
  103. } control;
  104. };
  105. DebugError d_err;
  106. DebugObject d_obj;
  107. } BSocksClient;
  108. struct BSocksClient_auth_info BSocksClient_auth_none (void);
  109. struct BSocksClient_auth_info BSocksClient_auth_password (const char *username, size_t username_len, const char *password, size_t password_len);
  110. /**
  111. * Initializes the object.
  112. *
  113. * This object connects to a SOCKS5 server and performs a CONNECT or UDP ASSOCIATE
  114. * operation. In any case, the object reports the BSOCKSCLIENT_EVENT_UP event via the
  115. * handler when the operation was completed successfully. In the case of CONNECT, the
  116. * user may then use the send and receive interfaces to exchange data through the
  117. * connection (@ref BSocksClient_GetSendInterface and @ref BSocksClient_GetRecvInterface).
  118. *
  119. * @param o the object
  120. * @param server_addr SOCKS5 server address
  121. * @param auth_info List of supported authentication methods and associated parameters.
  122. * Initialize these using functions such as BSocksClient_auth_none() and
  123. * BSocksClient_auth_password(). The pointer must remain valid while this object
  124. * exists, the data is not copied.
  125. * @param num_auth_info Number of the above. There should be at least one, otherwise it
  126. * certainly won't work.
  127. * @param dest_addr Address to send as DST.ADDR in the CONNECT or UDP ASSOCIATE request.
  128. * It is also possible to specify it later from the BSOCKSCLIENT_EVENT_CONNECTED
  129. * event callback using @ref BSocksClient_SetDestAddr; this is necessary for UDP
  130. * if the local TCP connection address must be known to bind the UDP socket.
  131. * @param udp false to perform a CONNECT, true to perform a UDP ASSOCIATE
  132. * @param handler handler for up and error events
  133. * @param user value passed to handler
  134. * @param reactor reactor we live in
  135. * @return 1 on success, 0 on failure
  136. */
  137. int BSocksClient_Init (BSocksClient *o, BAddr server_addr,
  138. const struct BSocksClient_auth_info *auth_info, size_t num_auth_info, BAddr dest_addr,
  139. bool udp, BSocksClient_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  140. /**
  141. * Frees the object.
  142. *
  143. * @param o the object
  144. */
  145. void BSocksClient_Free (BSocksClient *o);
  146. /**
  147. * Get the local address of the TCP socket for the SOCKS server connection.
  148. *
  149. * This may only be called after the BSOCKSCLIENT_EVENT_CONNECTED event was reported.
  150. *
  151. * @param o the object
  152. * @param local_addr On success the local address is returned here.
  153. * @return 1 on success, 0 on failure
  154. */
  155. int BSocksClient_GetLocalAddr (BSocksClient *o, BAddr *local_addr);
  156. /**
  157. * Set the DST.ADDR to send, overriding that specified in @ref BSocksClient_Init.
  158. *
  159. * The last chance to call this function is in the job closure of the
  160. * BSOCKSCLIENT_EVENT_CONNECTED event, this must not be called after that.
  161. *
  162. * @param o the object
  163. * @param dest_addr DST.ADDR to set.
  164. */
  165. void BSocksClient_SetDestAddr (BSocksClient *o, BAddr dest_addr);
  166. /**
  167. * Return the BND.ADDR that the SOCKS server reported.
  168. *
  169. * This may only be called after the BSOCKSCLIENT_EVENT_UP event was reported.
  170. * This address is needed for UDP ASSOCIATE because it is the address that the
  171. * client should send UDP packets to.
  172. *
  173. * @param o the object
  174. * @return The BND.ADDR, of type BADDR_TYPE_IPV4 or BADDR_TYPE_IPV6.
  175. */
  176. BAddr BSocksClient_GetBindAddr (BSocksClient *o);
  177. /**
  178. * Returns the send interface.
  179. * The object must be in up state. Additionally this must not be called if the
  180. * object was initialized in UDP ASSOCIATE mode.
  181. *
  182. * @param o the object
  183. * @return send interface
  184. */
  185. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o);
  186. /**
  187. * Returns the receive interface.
  188. * The object must be in up state. Additionally this must not be called if the
  189. * object was initialized in UDP ASSOCIATE mode.
  190. *
  191. * @param o the object
  192. * @return receive interface
  193. */
  194. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o);
  195. #endif