BSocksClient.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 method;
  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. typedef struct {
  84. BAddr dest_addr;
  85. BSocksClient_handler handler;
  86. void *user;
  87. BReactor *reactor;
  88. int state;
  89. BConnector connector;
  90. BConnection con;
  91. union {
  92. struct {
  93. PacketPassInterface *send_if;
  94. PacketStreamSender send_sender;
  95. StreamRecvInterface *recv_if;
  96. union {
  97. struct BSocksClient__client_hello client_hello;
  98. struct socks_server_hello server_hello;
  99. struct BSocksClient__request request;
  100. struct BSocksClient__reply reply;
  101. } msg;
  102. uint8_t *recv_dest;
  103. int recv_len;
  104. int recv_total;
  105. } control;
  106. };
  107. DebugError d_err;
  108. DebugObject d_obj;
  109. } BSocksClient;
  110. /**
  111. * Initializes the object.
  112. * The object is initialized in down state. The object must transition to up
  113. * state before the user may begin any I/O.
  114. *
  115. * @param o the object
  116. * @param server_addr SOCKS5 server address
  117. * @param dest_addr remote address
  118. * @param handler handler for up and error events
  119. * @param user value passed to handler
  120. * @param reactor reactor we live in
  121. * @return 1 on success, 0 on failure
  122. */
  123. int BSocksClient_Init (BSocksClient *o, BAddr server_addr, BAddr dest_addr, BSocksClient_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  124. /**
  125. * Frees the object.
  126. *
  127. * @param o the object
  128. */
  129. void BSocksClient_Free (BSocksClient *o);
  130. /**
  131. * Returns the send interface.
  132. * The object must be in up state.
  133. *
  134. * @param o the object
  135. * @return send interface
  136. */
  137. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o);
  138. /**
  139. * Returns the receive interface.
  140. * The object must be in up state.
  141. *
  142. * @param o the object
  143. * @return receive interface
  144. */
  145. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o);
  146. #endif