BSocksClient.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @file BSocksClient.c
  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. #include <misc/byteorder.h>
  23. #include <base/BLog.h>
  24. #include <socksclient/BSocksClient.h>
  25. #include <generated/blog_channel_BSocksClient.h>
  26. #define STATE_CONNECTING 1
  27. #define STATE_SENDING_HELLO 2
  28. #define STATE_SENT_HELLO 3
  29. #define STATE_SENDING_REQUEST 4
  30. #define STATE_SENT_REQUEST 5
  31. #define STATE_RECEIVED_REPLY_HEADER 6
  32. #define STATE_UP 7
  33. #define COMPONENT_SOURCE 1
  34. #define COMPONENT_SINK 2
  35. static void report_error (BSocksClient *o, int error);
  36. static void init_control_io (BSocksClient *o);
  37. static void free_control_io (BSocksClient *o);
  38. static void init_up_io (BSocksClient *o);
  39. static void free_up_io (BSocksClient *o);
  40. static void start_receive (BSocksClient *o, uint8_t *dest, int total);
  41. static void do_receive (BSocksClient *o);
  42. static void connector_handler (BSocksClient* o, int is_error);
  43. static void connection_handler (BSocksClient* o, int event);
  44. static void recv_handler_done (BSocksClient *o, int data_len);
  45. static void send_handler_done (BSocksClient *o);
  46. void report_error (BSocksClient *o, int error)
  47. {
  48. DEBUGERROR(&o->d_err, o->handler(o->user, error))
  49. }
  50. void init_control_io (BSocksClient *o)
  51. {
  52. // init receiving
  53. BConnection_RecvAsync_Init(&o->con);
  54. o->control.recv_if = BConnection_RecvAsync_GetIf(&o->con);
  55. StreamRecvInterface_Receiver_Init(o->control.recv_if, (StreamRecvInterface_handler_done)recv_handler_done, o);
  56. // init sending
  57. BConnection_SendAsync_Init(&o->con);
  58. PacketStreamSender_Init(&o->control.send_sender, BConnection_SendAsync_GetIf(&o->con), sizeof(o->control.msg), BReactor_PendingGroup(o->reactor));
  59. o->control.send_if = PacketStreamSender_GetInput(&o->control.send_sender);
  60. PacketPassInterface_Sender_Init(o->control.send_if, (PacketPassInterface_handler_done)send_handler_done, o);
  61. }
  62. void free_control_io (BSocksClient *o)
  63. {
  64. // free sending
  65. PacketStreamSender_Free(&o->control.send_sender);
  66. BConnection_SendAsync_Free(&o->con);
  67. // free receiving
  68. BConnection_RecvAsync_Free(&o->con);
  69. }
  70. void init_up_io (BSocksClient *o)
  71. {
  72. // init receiving
  73. BConnection_RecvAsync_Init(&o->con);
  74. // init sending
  75. BConnection_SendAsync_Init(&o->con);
  76. }
  77. void free_up_io (BSocksClient *o)
  78. {
  79. // free sending
  80. BConnection_SendAsync_Free(&o->con);
  81. // free receiving
  82. BConnection_RecvAsync_Free(&o->con);
  83. }
  84. void start_receive (BSocksClient *o, uint8_t *dest, int total)
  85. {
  86. ASSERT(total > 0)
  87. o->control.recv_dest = dest;
  88. o->control.recv_len = 0;
  89. o->control.recv_total = total;
  90. do_receive(o);
  91. }
  92. void do_receive (BSocksClient *o)
  93. {
  94. ASSERT(o->control.recv_len < o->control.recv_total)
  95. StreamRecvInterface_Receiver_Recv(o->control.recv_if, o->control.recv_dest + o->control.recv_len, o->control.recv_total - o->control.recv_len);
  96. }
  97. void connector_handler (BSocksClient* o, int is_error)
  98. {
  99. DebugObject_Access(&o->d_obj);
  100. ASSERT(o->state == STATE_CONNECTING)
  101. // check connection result
  102. if (is_error) {
  103. BLog(BLOG_ERROR, "connection failed");
  104. goto fail0;
  105. }
  106. // init connection
  107. if (!BConnection_Init(&o->con, BCONNECTION_SOURCE_CONNECTOR(&o->connector), o->reactor, o, (BConnection_handler)connection_handler)) {
  108. BLog(BLOG_ERROR, "BConnection_Init failed");
  109. goto fail0;
  110. }
  111. BLog(BLOG_DEBUG, "connected");
  112. // init control I/O
  113. init_control_io(o);
  114. // send hello
  115. o->control.msg.client_hello.header.ver = hton8(SOCKS_VERSION);
  116. o->control.msg.client_hello.header.nmethods = hton8(1);
  117. o->control.msg.client_hello.method.method = hton8(SOCKS_METHOD_NO_AUTHENTICATION_REQUIRED);
  118. PacketPassInterface_Sender_Send(o->control.send_if, (uint8_t *)&o->control.msg.client_hello, sizeof(o->control.msg.client_hello));
  119. // set state
  120. o->state = STATE_SENDING_HELLO;
  121. return;
  122. fail0:
  123. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  124. return;
  125. }
  126. void connection_handler (BSocksClient* o, int event)
  127. {
  128. DebugObject_Access(&o->d_obj);
  129. ASSERT(o->state != STATE_CONNECTING)
  130. if (o->state == STATE_UP && event == BCONNECTION_EVENT_RECVCLOSED) {
  131. report_error(o, BSOCKSCLIENT_EVENT_ERROR_CLOSED);
  132. return;
  133. }
  134. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  135. return;
  136. }
  137. void recv_handler_done (BSocksClient *o, int data_len)
  138. {
  139. ASSERT(data_len >= 0)
  140. ASSERT(data_len <= o->control.recv_total - o->control.recv_len)
  141. DebugObject_Access(&o->d_obj);
  142. o->control.recv_len += data_len;
  143. if (o->control.recv_len < o->control.recv_total) {
  144. do_receive(o);
  145. return;
  146. }
  147. switch (o->state) {
  148. case STATE_SENT_HELLO: {
  149. BLog(BLOG_DEBUG, "received hello");
  150. if (ntoh8(o->control.msg.server_hello.ver != SOCKS_VERSION)) {
  151. BLog(BLOG_NOTICE, "wrong version");
  152. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  153. return;
  154. }
  155. if (ntoh8(o->control.msg.server_hello.method != SOCKS_METHOD_NO_AUTHENTICATION_REQUIRED)) {
  156. BLog(BLOG_NOTICE, "wrong method");
  157. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  158. return;
  159. }
  160. // send request
  161. o->control.msg.request.header.ver = hton8(SOCKS_VERSION);
  162. o->control.msg.request.header.cmd = hton8(SOCKS_CMD_CONNECT);
  163. o->control.msg.request.header.rsv = hton8(0);
  164. int len = sizeof(o->control.msg.request.header);
  165. switch (o->dest_addr.type) {
  166. case BADDR_TYPE_IPV4:
  167. o->control.msg.request.header.atyp = hton8(SOCKS_ATYP_IPV4);
  168. o->control.msg.request.addr.ipv4.addr = o->dest_addr.ipv4.ip;
  169. o->control.msg.request.addr.ipv4.port = o->dest_addr.ipv4.port;
  170. len += sizeof(o->control.msg.request.addr.ipv4);
  171. break;
  172. case BADDR_TYPE_IPV6:
  173. o->control.msg.request.header.atyp = hton8(SOCKS_ATYP_IPV6);
  174. memcpy(o->control.msg.request.addr.ipv6.addr, o->dest_addr.ipv6.ip, sizeof(o->dest_addr.ipv6.ip));
  175. o->control.msg.request.addr.ipv6.port = o->dest_addr.ipv6.port;
  176. len += sizeof(o->control.msg.request.addr.ipv6);
  177. break;
  178. default:
  179. ASSERT(0);
  180. }
  181. PacketPassInterface_Sender_Send(o->control.send_if, (uint8_t *)&o->control.msg.request, len);
  182. // set state
  183. o->state = STATE_SENDING_REQUEST;
  184. } break;
  185. case STATE_SENT_REQUEST: {
  186. BLog(BLOG_DEBUG, "received reply header");
  187. if (ntoh8(o->control.msg.reply.header.ver) != SOCKS_VERSION) {
  188. BLog(BLOG_NOTICE, "wrong version");
  189. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  190. return;
  191. }
  192. if (ntoh8(o->control.msg.reply.header.rep) != SOCKS_REP_SUCCEEDED) {
  193. BLog(BLOG_NOTICE, "reply not successful");
  194. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  195. return;
  196. }
  197. int addr_len;
  198. switch (ntoh8(o->control.msg.reply.header.atyp)) {
  199. case SOCKS_ATYP_IPV4:
  200. addr_len = sizeof(o->control.msg.reply.addr.ipv4);
  201. break;
  202. case SOCKS_ATYP_IPV6:
  203. addr_len = sizeof(o->control.msg.reply.addr.ipv6);
  204. break;
  205. default:
  206. BLog(BLOG_NOTICE, "reply has unknown address type");
  207. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  208. return;
  209. }
  210. // receive the rest of the reply
  211. start_receive(o, (uint8_t *)&o->control.msg.reply.addr, addr_len);
  212. // set state
  213. o->state = STATE_RECEIVED_REPLY_HEADER;
  214. } break;
  215. case STATE_RECEIVED_REPLY_HEADER: {
  216. BLog(BLOG_DEBUG, "received reply rest");
  217. // free control I/O
  218. free_control_io(o);
  219. // init up I/O
  220. init_up_io(o);
  221. // set state
  222. o->state = STATE_UP;
  223. // call handler
  224. o->handler(o->user, BSOCKSCLIENT_EVENT_UP);
  225. return;
  226. } break;
  227. default:
  228. ASSERT(0);
  229. }
  230. }
  231. void send_handler_done (BSocksClient *o)
  232. {
  233. DebugObject_Access(&o->d_obj);
  234. switch (o->state) {
  235. case STATE_SENDING_HELLO: {
  236. BLog(BLOG_DEBUG, "sent hello");
  237. // receive hello
  238. start_receive(o, (uint8_t *)&o->control.msg.server_hello, sizeof(o->control.msg.server_hello));
  239. // set state
  240. o->state = STATE_SENT_HELLO;
  241. } break;
  242. case STATE_SENDING_REQUEST: {
  243. BLog(BLOG_DEBUG, "sent request");
  244. // receive reply header
  245. start_receive(o, (uint8_t *)&o->control.msg.reply.header, sizeof(o->control.msg.reply.header));
  246. // set state
  247. o->state = STATE_SENT_REQUEST;
  248. } break;
  249. default:
  250. ASSERT(0);
  251. }
  252. }
  253. int BSocksClient_Init (BSocksClient *o, BAddr server_addr, BAddr dest_addr, BSocksClient_handler handler, void *user, BReactor *reactor)
  254. {
  255. ASSERT(!BAddr_IsInvalid(&server_addr))
  256. ASSERT(dest_addr.type == BADDR_TYPE_IPV4 || dest_addr.type == BADDR_TYPE_IPV6)
  257. // init arguments
  258. o->dest_addr = dest_addr;
  259. o->handler = handler;
  260. o->user = user;
  261. o->reactor = reactor;
  262. // init connector
  263. if (!BConnector_Init(&o->connector, server_addr, o->reactor, o, (BConnector_handler)connector_handler)) {
  264. BLog(BLOG_ERROR, "BConnector_Init failed");
  265. goto fail0;
  266. }
  267. // set state
  268. o->state = STATE_CONNECTING;
  269. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  270. DebugObject_Init(&o->d_obj);
  271. return 1;
  272. fail0:
  273. return 0;
  274. }
  275. void BSocksClient_Free (BSocksClient *o)
  276. {
  277. DebugObject_Free(&o->d_obj);
  278. DebugError_Free(&o->d_err);
  279. if (o->state != STATE_CONNECTING) {
  280. if (o->state == STATE_UP) {
  281. // free up I/O
  282. free_up_io(o);
  283. } else {
  284. ASSERT(o->state == STATE_SENDING_HELLO || o->state == STATE_SENT_HELLO ||
  285. o->state == STATE_SENDING_REQUEST || o->state == STATE_SENT_REQUEST ||
  286. o->state == STATE_RECEIVED_REPLY_HEADER
  287. )
  288. // free control I/O
  289. free_control_io(o);
  290. }
  291. // free connection
  292. BConnection_Free(&o->con);
  293. }
  294. // free connector
  295. BConnector_Free(&o->connector);
  296. }
  297. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o)
  298. {
  299. ASSERT(o->state == STATE_UP)
  300. DebugObject_Access(&o->d_obj);
  301. return BConnection_SendAsync_GetIf(&o->con);
  302. }
  303. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o)
  304. {
  305. ASSERT(o->state == STATE_UP)
  306. DebugObject_Access(&o->d_obj);
  307. return BConnection_RecvAsync_GetIf(&o->con);
  308. }