BSocksClient.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file BSocksClient.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * SOCKS5 client. TCP only, no authentication.
  25. */
  26. #ifndef BADVPN_SOCKS_BSOCKSCLIENT_H
  27. #define BADVPN_SOCKS_BSOCKSCLIENT_H
  28. #include <stdint.h>
  29. #include <misc/debug.h>
  30. #include <misc/debugerror.h>
  31. #include <misc/socks_proto.h>
  32. #include <base/DebugObject.h>
  33. #include <system/BSocket.h>
  34. #include <flow/PacketStreamSender.h>
  35. #include <flowextra/StreamSocketSink.h>
  36. #include <flowextra/StreamSocketSource.h>
  37. #define BSOCKSCLIENT_EVENT_ERROR 1
  38. #define BSOCKSCLIENT_EVENT_UP 2
  39. #define BSOCKSCLIENT_EVENT_ERROR_CLOSED 3
  40. /**
  41. * Handler for events generated by the SOCKS client.
  42. *
  43. * @param user as in {@link BSocksClient_Init}
  44. * @param event event type. One of BSOCKSCLIENT_EVENT_ERROR, BSOCKSCLIENT_EVENT_UP
  45. * and BSOCKSCLIENT_EVENT_ERROR_CLOSED.
  46. * If event is BSOCKSCLIENT_EVENT_UP, the object was previously in down
  47. * state and has transitioned to up state; I/O can be done from this point on.
  48. * If event is BSOCKSCLIENT_EVENT_ERROR or BSOCKSCLIENT_EVENT_ERROR_CLOSED,
  49. * the object must be freed from within the job closure of this handler,
  50. * and no further I/O must be attempted.
  51. */
  52. typedef void (*BSocksClient_handler) (void *user, int event);
  53. typedef struct {
  54. BAddr dest_addr;
  55. BSocksClient_handler handler;
  56. void *user;
  57. BReactor *reactor;
  58. int state;
  59. FlowErrorDomain domain;
  60. BSocket sock;
  61. union {
  62. struct {
  63. PacketPassInterface *send_if;
  64. PacketStreamSender send_sender;
  65. StreamSocketSink send_sink;
  66. StreamSocketSource recv_source;
  67. StreamRecvInterface *recv_if;
  68. union {
  69. struct {
  70. struct socks_client_hello_header header;
  71. struct socks_client_hello_method method;
  72. } __attribute__((packed)) client_hello;
  73. struct socks_server_hello server_hello;
  74. struct {
  75. struct socks_request_header header;
  76. union {
  77. struct socks_addr_ipv4 ipv4;
  78. struct socks_addr_ipv6 ipv6;
  79. } addr;
  80. } __attribute__((packed)) request;
  81. struct {
  82. struct socks_reply_header header;
  83. union {
  84. struct socks_addr_ipv4 ipv4;
  85. struct socks_addr_ipv6 ipv6;
  86. } addr;
  87. } __attribute__((packed)) reply;
  88. } msg;
  89. uint8_t *recv_dest;
  90. int recv_len;
  91. int recv_total;
  92. } control;
  93. struct {
  94. StreamSocketSink send_sink;
  95. StreamSocketSource recv_source;
  96. } up;
  97. };
  98. DebugError d_err;
  99. DebugObject d_obj;
  100. } BSocksClient;
  101. /**
  102. * Initializes the object.
  103. * The object is initialized in down state. The object must transition to up
  104. * state before the user may begin any I/O.
  105. *
  106. * @param o the object
  107. * @param server_addr SOCKS5 server address
  108. * @param dest_addr remote address
  109. * @param handler handler for up and error events
  110. * @param user value passed to handler
  111. * @param reactor reactor we live in
  112. * @return 1 on success, 0 on failure
  113. */
  114. int BSocksClient_Init (BSocksClient *o, BAddr server_addr, BAddr dest_addr, 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