BSocksClient.h 5.0 KB

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