BSocksClient.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <stdint.h>
  36. #include <misc/debug.h>
  37. #include <misc/debugerror.h>
  38. #include <misc/socks_proto.h>
  39. #include <misc/packed.h>
  40. #include <base/DebugObject.h>
  41. #include <system/BConnection.h>
  42. #include <flow/PacketStreamSender.h>
  43. #define BSOCKSCLIENT_EVENT_ERROR 1
  44. #define BSOCKSCLIENT_EVENT_UP 2
  45. #define BSOCKSCLIENT_EVENT_ERROR_CLOSED 3
  46. /**
  47. * Handler for events generated by the SOCKS client.
  48. *
  49. * @param user as in {@link BSocksClient_Init}
  50. * @param event event type. One of BSOCKSCLIENT_EVENT_ERROR, BSOCKSCLIENT_EVENT_UP
  51. * and BSOCKSCLIENT_EVENT_ERROR_CLOSED.
  52. * If event is BSOCKSCLIENT_EVENT_UP, the object was previously in down
  53. * state and has transitioned to up state; I/O can be done from this point on.
  54. * If event is BSOCKSCLIENT_EVENT_ERROR or BSOCKSCLIENT_EVENT_ERROR_CLOSED,
  55. * the object must be freed from within the job closure of this handler,
  56. * and no further I/O must be attempted.
  57. */
  58. typedef void (*BSocksClient_handler) (void *user, int event);
  59. B_START_PACKED
  60. struct BSocksClient__client_hello {
  61. struct socks_client_hello_header header;
  62. struct socks_client_hello_method methods[];
  63. } B_PACKED;
  64. B_END_PACKED
  65. B_START_PACKED
  66. struct BSocksClient__request {
  67. struct socks_request_header header;
  68. union {
  69. struct socks_addr_ipv4 ipv4;
  70. struct socks_addr_ipv6 ipv6;
  71. } addr;
  72. } B_PACKED;
  73. B_END_PACKED
  74. B_START_PACKED
  75. struct BSocksClient__reply {
  76. struct socks_reply_header header;
  77. union {
  78. struct socks_addr_ipv4 ipv4;
  79. struct socks_addr_ipv6 ipv6;
  80. } addr;
  81. } B_PACKED;
  82. B_END_PACKED
  83. struct BSocksClient_auth_info {
  84. int auth_type;
  85. union {
  86. struct {
  87. const char *username;
  88. size_t username_len;
  89. const char *password;
  90. size_t password_len;
  91. } password;
  92. };
  93. };
  94. typedef struct {
  95. const struct BSocksClient_auth_info *auth_info;
  96. size_t num_auth_info;
  97. BAddr dest_addr;
  98. BSocksClient_handler handler;
  99. void *user;
  100. BReactor *reactor;
  101. int state;
  102. char *buffer;
  103. BConnector connector;
  104. BConnection con;
  105. union {
  106. struct {
  107. PacketPassInterface *send_if;
  108. PacketStreamSender send_sender;
  109. StreamRecvInterface *recv_if;
  110. uint8_t *recv_dest;
  111. int recv_len;
  112. int recv_total;
  113. } control;
  114. };
  115. DebugError d_err;
  116. DebugObject d_obj;
  117. } BSocksClient;
  118. struct BSocksClient_auth_info BSocksClient_auth_none (void);
  119. struct BSocksClient_auth_info BSocksClient_auth_password (const char *username, size_t username_len, const char *password, size_t password_len);
  120. /**
  121. * Initializes the object.
  122. * The object is initialized in down state. The object must transition to up
  123. * state before the user may begin any I/O.
  124. *
  125. * @param o the object
  126. * @param server_addr SOCKS5 server address
  127. * @param dest_addr remote address
  128. * @param handler handler for up and error events
  129. * @param user value passed to handler
  130. * @param reactor reactor we live in
  131. * @return 1 on success, 0 on failure
  132. */
  133. int BSocksClient_Init (BSocksClient *o,
  134. BAddr server_addr, const struct BSocksClient_auth_info *auth_info, size_t num_auth_info,
  135. BAddr dest_addr, BSocksClient_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  136. /**
  137. * Frees the object.
  138. *
  139. * @param o the object
  140. */
  141. void BSocksClient_Free (BSocksClient *o);
  142. /**
  143. * Returns the send interface.
  144. * The object must be in up state.
  145. *
  146. * @param o the object
  147. * @return send interface
  148. */
  149. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o);
  150. /**
  151. * Returns the receive interface.
  152. * The object must be in up state.
  153. *
  154. * @param o the object
  155. * @return receive interface
  156. */
  157. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o);
  158. #endif