BSocksClient.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <system/BConnection.h>
  44. #include <flow/PacketStreamSender.h>
  45. #define BSOCKSCLIENT_EVENT_ERROR 1
  46. #define BSOCKSCLIENT_EVENT_UP 2
  47. #define BSOCKSCLIENT_EVENT_ERROR_CLOSED 3
  48. /**
  49. * Handler for events generated by the SOCKS client.
  50. *
  51. * @param user as in {@link BSocksClient_Init}
  52. * @param event event type. One of BSOCKSCLIENT_EVENT_ERROR, BSOCKSCLIENT_EVENT_UP
  53. * and BSOCKSCLIENT_EVENT_ERROR_CLOSED.
  54. * If event is BSOCKSCLIENT_EVENT_UP, the object was previously in down
  55. * state and has transitioned to up state; I/O can be done from this point on.
  56. * If event is BSOCKSCLIENT_EVENT_ERROR or BSOCKSCLIENT_EVENT_ERROR_CLOSED,
  57. * the object must be freed from within the job closure of this handler,
  58. * and no further I/O must be attempted.
  59. */
  60. typedef void (*BSocksClient_handler) (void *user, int event);
  61. struct BSocksClient_auth_info {
  62. int auth_type;
  63. union {
  64. struct {
  65. const char *username;
  66. size_t username_len;
  67. const char *password;
  68. size_t password_len;
  69. } password;
  70. };
  71. };
  72. typedef struct {
  73. const struct BSocksClient_auth_info *auth_info;
  74. size_t num_auth_info;
  75. BAddr dest_addr;
  76. bool udp;
  77. BAddr bind_addr;
  78. BSocksClient_handler handler;
  79. void *user;
  80. BReactor *reactor;
  81. int state;
  82. char *buffer;
  83. BConnector connector;
  84. BConnection con;
  85. union {
  86. struct {
  87. PacketPassInterface *send_if;
  88. PacketStreamSender send_sender;
  89. StreamRecvInterface *recv_if;
  90. uint8_t *recv_dest;
  91. int recv_len;
  92. int recv_total;
  93. } control;
  94. };
  95. DebugError d_err;
  96. DebugObject d_obj;
  97. } BSocksClient;
  98. struct BSocksClient_auth_info BSocksClient_auth_none (void);
  99. struct BSocksClient_auth_info BSocksClient_auth_password (const char *username, size_t username_len, const char *password, size_t password_len);
  100. /**
  101. * Initializes the object.
  102. * The object is initialized in down state. The object must transition to up
  103. * state before the user may begin any I/O.
  104. *
  105. * @param o the object
  106. * @param server_addr SOCKS5 server address
  107. * @param auth_info List of supported authentication methods and associated parameters.
  108. * Initialize these using functions such as BSocksClient_auth_none() and
  109. * BSocksClient_auth_password(). The pointer must remain valid while this object
  110. * exists, the data is not copied.
  111. * @param num_auth_info Number of the above. There should be at least one, otherwise it
  112. * certainly won't work.
  113. * @param dest_addr remote address
  114. * @param udp whether to do UDP ASSOCIATE instead of CONNECT
  115. * @param handler handler for up and error events
  116. * @param user value passed to handler
  117. * @param reactor reactor we live in
  118. * @return 1 on success, 0 on failure
  119. */
  120. int BSocksClient_Init (BSocksClient *o,
  121. BAddr server_addr, const struct BSocksClient_auth_info *auth_info, size_t num_auth_info,
  122. BAddr dest_addr, bool udp, BSocksClient_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  123. /**
  124. * Frees the object.
  125. *
  126. * @param o the object
  127. */
  128. void BSocksClient_Free (BSocksClient *o);
  129. /**
  130. * Return the bind address that the SOCKS server reported.
  131. * The object must be in up state. The bind address is needed for UDP ASSOCIATE
  132. * because it is the address that the client should send UDP packets to.
  133. *
  134. * @param o the object
  135. * @return The bind address, of type BADDR_TYPE_IPV4 or BADDR_TYPE_IPV6.
  136. */
  137. BAddr BSocksClient_GetBindAddr (BSocksClient *o);
  138. /**
  139. * Returns the send interface.
  140. * The object must be in up state. Additionally this must not be called if the
  141. * object was initialized in UDP ASSOCIATE mode.
  142. *
  143. * @param o the object
  144. * @return send interface
  145. */
  146. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o);
  147. /**
  148. * Returns the receive interface.
  149. * The object must be in up state. Additionally this must not be called if the
  150. * object was initialized in UDP ASSOCIATE mode.
  151. *
  152. * @param o the object
  153. * @return receive interface
  154. */
  155. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o);
  156. #endif