BSocksClient.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. struct BSocksClient_auth_info {
  60. int auth_type;
  61. union {
  62. struct {
  63. const char *username;
  64. size_t username_len;
  65. const char *password;
  66. size_t password_len;
  67. } password;
  68. };
  69. };
  70. typedef struct {
  71. const struct BSocksClient_auth_info *auth_info;
  72. size_t num_auth_info;
  73. BAddr dest_addr;
  74. BSocksClient_handler handler;
  75. void *user;
  76. BReactor *reactor;
  77. int state;
  78. char *buffer;
  79. BConnector connector;
  80. BConnection con;
  81. union {
  82. struct {
  83. PacketPassInterface *send_if;
  84. PacketStreamSender send_sender;
  85. StreamRecvInterface *recv_if;
  86. uint8_t *recv_dest;
  87. int recv_len;
  88. int recv_total;
  89. } control;
  90. };
  91. DebugError d_err;
  92. DebugObject d_obj;
  93. } BSocksClient;
  94. struct BSocksClient_auth_info BSocksClient_auth_none (void);
  95. struct BSocksClient_auth_info BSocksClient_auth_password (const char *username, size_t username_len, const char *password, size_t password_len);
  96. /**
  97. * Initializes the object.
  98. * The object is initialized in down state. The object must transition to up
  99. * state before the user may begin any I/O.
  100. *
  101. * @param o the object
  102. * @param server_addr SOCKS5 server address
  103. * @param dest_addr remote address
  104. * @param handler handler for up and error events
  105. * @param user value passed to handler
  106. * @param reactor reactor we live in
  107. * @return 1 on success, 0 on failure
  108. */
  109. int BSocksClient_Init (BSocksClient *o,
  110. BAddr server_addr, const struct BSocksClient_auth_info *auth_info, size_t num_auth_info,
  111. BAddr dest_addr, BSocksClient_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  112. /**
  113. * Frees the object.
  114. *
  115. * @param o the object
  116. */
  117. void BSocksClient_Free (BSocksClient *o);
  118. /**
  119. * Returns the send interface.
  120. * The object must be in up state.
  121. *
  122. * @param o the object
  123. * @return send interface
  124. */
  125. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o);
  126. /**
  127. * Returns the receive interface.
  128. * The object must be in up state.
  129. *
  130. * @param o the object
  131. * @return receive interface
  132. */
  133. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o);
  134. #endif