BSocksClient.h 4.3 KB

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