BSocksClient.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /**
  2. * @file BSocksClient.c
  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. #include <string.h>
  30. #include <misc/byteorder.h>
  31. #include <misc/balloc.h>
  32. #include <base/BLog.h>
  33. #include <socksclient/BSocksClient.h>
  34. #include <generated/blog_channel_BSocksClient.h>
  35. #define STATE_CONNECTING 1
  36. #define STATE_CONNECTED_HANDLER 2
  37. #define STATE_SENDING_HELLO 3
  38. #define STATE_SENT_HELLO 4
  39. #define STATE_SENDING_PASSWORD 5
  40. #define STATE_SENT_PASSWORD 6
  41. #define STATE_SENDING_REQUEST 7
  42. #define STATE_SENT_REQUEST 8
  43. #define STATE_RECEIVED_REPLY_HEADER 9
  44. #define STATE_UP 10
  45. static void report_error (BSocksClient *o, int error);
  46. static void init_control_io (BSocksClient *o);
  47. static void free_control_io (BSocksClient *o);
  48. static void init_up_io (BSocksClient *o);
  49. static void free_up_io (BSocksClient *o);
  50. static int reserve_buffer (BSocksClient *o, bsize_t size);
  51. static void start_receive (BSocksClient *o, uint8_t *dest, int total);
  52. static void do_receive (BSocksClient *o);
  53. static void connector_handler (BSocksClient* o, int is_error);
  54. static void connection_handler (BSocksClient* o, int event);
  55. static void continue_job_handler (BSocksClient *o);
  56. static void recv_handler_done (BSocksClient *o, int data_len);
  57. static void send_handler_done (BSocksClient *o);
  58. static void auth_finished (BSocksClient *p);
  59. void report_error (BSocksClient *o, int error)
  60. {
  61. DEBUGERROR(&o->d_err, o->handler(o->user, error))
  62. }
  63. void init_control_io (BSocksClient *o)
  64. {
  65. // init receiving
  66. BConnection_RecvAsync_Init(&o->con);
  67. o->control.recv_if = BConnection_RecvAsync_GetIf(&o->con);
  68. StreamRecvInterface_Receiver_Init(o->control.recv_if, (StreamRecvInterface_handler_done)recv_handler_done, o);
  69. // init sending
  70. BConnection_SendAsync_Init(&o->con);
  71. PacketStreamSender_Init(&o->control.send_sender, BConnection_SendAsync_GetIf(&o->con), INT_MAX, BReactor_PendingGroup(o->reactor));
  72. o->control.send_if = PacketStreamSender_GetInput(&o->control.send_sender);
  73. PacketPassInterface_Sender_Init(o->control.send_if, (PacketPassInterface_handler_done)send_handler_done, o);
  74. }
  75. void free_control_io (BSocksClient *o)
  76. {
  77. // free sending
  78. PacketStreamSender_Free(&o->control.send_sender);
  79. BConnection_SendAsync_Free(&o->con);
  80. // free receiving
  81. BConnection_RecvAsync_Free(&o->con);
  82. }
  83. void init_up_io (BSocksClient *o)
  84. {
  85. // init receiving
  86. BConnection_RecvAsync_Init(&o->con);
  87. // init sending
  88. BConnection_SendAsync_Init(&o->con);
  89. }
  90. void free_up_io (BSocksClient *o)
  91. {
  92. // free sending
  93. BConnection_SendAsync_Free(&o->con);
  94. // free receiving
  95. BConnection_RecvAsync_Free(&o->con);
  96. }
  97. int reserve_buffer (BSocksClient *o, bsize_t size)
  98. {
  99. if (size.is_overflow) {
  100. BLog(BLOG_ERROR, "size overflow");
  101. return 0;
  102. }
  103. char *buffer = (char *)BRealloc(o->buffer, size.value);
  104. if (!buffer) {
  105. BLog(BLOG_ERROR, "BRealloc failed");
  106. return 0;
  107. }
  108. o->buffer = buffer;
  109. return 1;
  110. }
  111. void start_receive (BSocksClient *o, uint8_t *dest, int total)
  112. {
  113. ASSERT(total > 0)
  114. o->control.recv_dest = dest;
  115. o->control.recv_len = 0;
  116. o->control.recv_total = total;
  117. do_receive(o);
  118. }
  119. void do_receive (BSocksClient *o)
  120. {
  121. ASSERT(o->control.recv_len < o->control.recv_total)
  122. StreamRecvInterface_Receiver_Recv(o->control.recv_if, o->control.recv_dest + o->control.recv_len, o->control.recv_total - o->control.recv_len);
  123. }
  124. void connector_handler (BSocksClient* o, int is_error)
  125. {
  126. DebugObject_Access(&o->d_obj);
  127. ASSERT(o->state == STATE_CONNECTING)
  128. // check connection result
  129. if (is_error) {
  130. BLog(BLOG_ERROR, "connection failed");
  131. goto fail0;
  132. }
  133. // init connection
  134. if (!BConnection_Init(&o->con, BConnection_source_connector(&o->connector), o->reactor, o, (BConnection_handler)connection_handler)) {
  135. BLog(BLOG_ERROR, "BConnection_Init failed");
  136. goto fail0;
  137. }
  138. BLog(BLOG_DEBUG, "connected");
  139. // init control I/O
  140. init_control_io(o);
  141. // go to STATE_CONNECTED_HANDLER and set the continue job in order to continue
  142. // in continue_job_handler
  143. o->state = STATE_CONNECTED_HANDLER;
  144. BPending_Set(&o->continue_job);
  145. // call the handler with the connected event
  146. o->handler(o->user, BSOCKSCLIENT_EVENT_CONNECTED);
  147. return;
  148. fail0:
  149. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  150. return;
  151. }
  152. void connection_handler (BSocksClient* o, int event)
  153. {
  154. DebugObject_Access(&o->d_obj);
  155. ASSERT(o->state != STATE_CONNECTING)
  156. if (o->state == STATE_UP && event == BCONNECTION_EVENT_RECVCLOSED) {
  157. report_error(o, BSOCKSCLIENT_EVENT_ERROR_CLOSED);
  158. return;
  159. }
  160. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  161. return;
  162. }
  163. void continue_job_handler (BSocksClient *o)
  164. {
  165. DebugObject_Access(&o->d_obj);
  166. ASSERT(o->state == STATE_CONNECTED_HANDLER)
  167. // check number of methods
  168. if (o->num_auth_info == 0 || o->num_auth_info > 255) {
  169. BLog(BLOG_ERROR, "invalid number of authentication methods");
  170. goto fail0;
  171. }
  172. // allocate buffer for sending hello
  173. bsize_t size = bsize_add(
  174. bsize_fromsize(sizeof(struct socks_client_hello_header)),
  175. bsize_mul(
  176. bsize_fromsize(o->num_auth_info),
  177. bsize_fromsize(sizeof(struct socks_client_hello_method))
  178. )
  179. );
  180. if (!reserve_buffer(o, size)) {
  181. goto fail0;
  182. }
  183. // write hello header
  184. struct socks_client_hello_header header;
  185. header.ver = hton8(SOCKS_VERSION);
  186. header.nmethods = hton8(o->num_auth_info);
  187. memcpy(o->buffer, &header, sizeof(header));
  188. // write hello methods
  189. for (size_t i = 0; i < o->num_auth_info; i++) {
  190. struct socks_client_hello_method method;
  191. method.method = hton8(o->auth_info[i].auth_type);
  192. memcpy(o->buffer + sizeof(header) + i * sizeof(method), &method, sizeof(method));
  193. }
  194. // send
  195. PacketPassInterface_Sender_Send(o->control.send_if, (uint8_t *)o->buffer, size.value);
  196. // set state
  197. o->state = STATE_SENDING_HELLO;
  198. return;
  199. fail0:
  200. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  201. return;
  202. }
  203. void recv_handler_done (BSocksClient *o, int data_len)
  204. {
  205. ASSERT(data_len >= 0)
  206. ASSERT(data_len <= o->control.recv_total - o->control.recv_len)
  207. DebugObject_Access(&o->d_obj);
  208. o->control.recv_len += data_len;
  209. if (o->control.recv_len < o->control.recv_total) {
  210. do_receive(o);
  211. return;
  212. }
  213. switch (o->state) {
  214. case STATE_SENT_HELLO: {
  215. BLog(BLOG_DEBUG, "received hello");
  216. struct socks_server_hello imsg;
  217. memcpy(&imsg, o->buffer, sizeof(imsg));
  218. if (ntoh8(imsg.ver) != SOCKS_VERSION) {
  219. BLog(BLOG_NOTICE, "wrong version");
  220. goto fail;
  221. }
  222. size_t auth_index;
  223. for (auth_index = 0; auth_index < o->num_auth_info; auth_index++) {
  224. if (o->auth_info[auth_index].auth_type == ntoh8(imsg.method)) {
  225. break;
  226. }
  227. }
  228. if (auth_index == o->num_auth_info) {
  229. BLog(BLOG_NOTICE, "server didn't accept any authentication method");
  230. goto fail;
  231. }
  232. const struct BSocksClient_auth_info *ai = &o->auth_info[auth_index];
  233. switch (ai->auth_type) {
  234. case SOCKS_METHOD_NO_AUTHENTICATION_REQUIRED: {
  235. BLog(BLOG_DEBUG, "no authentication");
  236. auth_finished(o);
  237. } break;
  238. case SOCKS_METHOD_USERNAME_PASSWORD: {
  239. BLog(BLOG_DEBUG, "password authentication");
  240. if (ai->password.username_len == 0 || ai->password.username_len > 255 ||
  241. ai->password.password_len == 0 || ai->password.password_len > 255
  242. ) {
  243. BLog(BLOG_NOTICE, "invalid username/password length");
  244. goto fail;
  245. }
  246. // allocate password packet
  247. bsize_t size = bsize_fromsize(1 + 1 + ai->password.username_len + 1 + ai->password.password_len);
  248. if (!reserve_buffer(o, size)) {
  249. goto fail;
  250. }
  251. // write password packet
  252. char *ptr = o->buffer;
  253. *ptr++ = 1;
  254. *ptr++ = ai->password.username_len;
  255. memcpy(ptr, ai->password.username, ai->password.username_len);
  256. ptr += ai->password.username_len;
  257. *ptr++ = ai->password.password_len;
  258. memcpy(ptr, ai->password.password, ai->password.password_len);
  259. ptr += ai->password.password_len;
  260. // start sending
  261. PacketPassInterface_Sender_Send(o->control.send_if, (uint8_t *)o->buffer, size.value);
  262. // set state
  263. o->state = STATE_SENDING_PASSWORD;
  264. } break;
  265. default: ASSERT(0);
  266. }
  267. } break;
  268. case STATE_SENT_REQUEST: {
  269. BLog(BLOG_DEBUG, "received reply header");
  270. struct socks_reply_header imsg;
  271. memcpy(&imsg, o->buffer, sizeof(imsg));
  272. if (ntoh8(imsg.ver) != SOCKS_VERSION) {
  273. BLog(BLOG_NOTICE, "wrong version");
  274. goto fail;
  275. }
  276. if (ntoh8(imsg.rep) != SOCKS_REP_SUCCEEDED) {
  277. BLog(BLOG_NOTICE, "reply not successful");
  278. goto fail;
  279. }
  280. int addr_len;
  281. switch (ntoh8(imsg.atyp)) {
  282. case SOCKS_ATYP_IPV4:
  283. o->bind_addr.type = BADDR_TYPE_IPV4;
  284. addr_len = sizeof(struct socks_addr_ipv4);
  285. break;
  286. case SOCKS_ATYP_IPV6:
  287. o->bind_addr.type = BADDR_TYPE_IPV6;
  288. addr_len = sizeof(struct socks_addr_ipv6);
  289. break;
  290. default:
  291. BLog(BLOG_NOTICE, "reply has unknown address type");
  292. goto fail;
  293. }
  294. // receive the rest of the reply
  295. start_receive(o, (uint8_t *)o->buffer + sizeof(imsg), addr_len);
  296. // set state
  297. o->state = STATE_RECEIVED_REPLY_HEADER;
  298. } break;
  299. case STATE_SENT_PASSWORD: {
  300. BLog(BLOG_DEBUG, "received password reply");
  301. if (o->buffer[0] != 1) {
  302. BLog(BLOG_NOTICE, "password reply has unknown version");
  303. goto fail;
  304. }
  305. if (o->buffer[1] != 0) {
  306. BLog(BLOG_NOTICE, "password reply is negative");
  307. goto fail;
  308. }
  309. auth_finished(o);
  310. } break;
  311. case STATE_RECEIVED_REPLY_HEADER: {
  312. BLog(BLOG_DEBUG, "received reply rest");
  313. // Record the address of the new socket bound by the server.
  314. // For a CONNECT command, this is the address of the TCP client socket to dest_addr.
  315. // Knowing this address is usually not important.
  316. // For a UDP_ASSOCIATE command, this is the UDP address to which to send SOCKS UDP.
  317. // Recording this address is a prerequisite to send traffic on a SOCKS-UDP association.
  318. void *addr_buffer = o->buffer + sizeof(struct socks_reply_header);
  319. switch (o->bind_addr.type) {
  320. case BADDR_TYPE_IPV4: {
  321. struct socks_addr_ipv4 ip4;
  322. memcpy(&ip4, addr_buffer, sizeof(ip4));
  323. o->bind_addr.ipv4.ip = ip4.addr;
  324. o->bind_addr.ipv4.port = ip4.port;
  325. } break;
  326. case BADDR_TYPE_IPV6: {
  327. struct socks_addr_ipv6 ip6;
  328. memcpy(&ip6, addr_buffer, sizeof(ip6));
  329. memcpy(o->bind_addr.ipv6.ip, ip6.addr, sizeof(ip6.addr));
  330. o->bind_addr.ipv6.port = ip6.port;
  331. } break;
  332. default: ASSERT(0);
  333. }
  334. // free buffer
  335. BFree(o->buffer);
  336. o->buffer = NULL;
  337. // free control I/O
  338. free_control_io(o);
  339. // init up I/O
  340. // Initializing this is not needed for UDP ASSOCIATE but it doesn't hurt.
  341. // We anyway don't allow the user to use these interfaces in that case.
  342. init_up_io(o);
  343. // set state
  344. o->state = STATE_UP;
  345. // call handler
  346. o->handler(o->user, BSOCKSCLIENT_EVENT_UP);
  347. return;
  348. } break;
  349. default:
  350. ASSERT(0);
  351. }
  352. return;
  353. fail:
  354. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  355. }
  356. void send_handler_done (BSocksClient *o)
  357. {
  358. DebugObject_Access(&o->d_obj);
  359. ASSERT(o->buffer)
  360. switch (o->state) {
  361. case STATE_SENDING_HELLO: {
  362. BLog(BLOG_DEBUG, "sent hello");
  363. // allocate buffer for receiving hello
  364. bsize_t size = bsize_fromsize(sizeof(struct socks_server_hello));
  365. if (!reserve_buffer(o, size)) {
  366. goto fail;
  367. }
  368. // receive hello
  369. start_receive(o, (uint8_t *)o->buffer, size.value);
  370. // set state
  371. o->state = STATE_SENT_HELLO;
  372. } break;
  373. case STATE_SENDING_REQUEST: {
  374. BLog(BLOG_DEBUG, "sent request");
  375. // allocate buffer for receiving reply
  376. bsize_t size = bsize_add(
  377. bsize_fromsize(sizeof(struct socks_reply_header)),
  378. bsize_max(bsize_fromsize(sizeof(struct socks_addr_ipv4)), bsize_fromsize(sizeof(struct socks_addr_ipv6)))
  379. );
  380. if (!reserve_buffer(o, size)) {
  381. goto fail;
  382. }
  383. // receive reply header
  384. start_receive(o, (uint8_t *)o->buffer, sizeof(struct socks_reply_header));
  385. // set state
  386. o->state = STATE_SENT_REQUEST;
  387. } break;
  388. case STATE_SENDING_PASSWORD: {
  389. BLog(BLOG_DEBUG, "send password");
  390. // allocate buffer for receiving reply
  391. bsize_t size = bsize_fromsize(2);
  392. if (!reserve_buffer(o, size)) {
  393. goto fail;
  394. }
  395. // receive reply header
  396. start_receive(o, (uint8_t *)o->buffer, size.value);
  397. // set state
  398. o->state = STATE_SENT_PASSWORD;
  399. } break;
  400. default:
  401. ASSERT(0);
  402. }
  403. return;
  404. fail:
  405. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  406. }
  407. void auth_finished (BSocksClient *o)
  408. {
  409. // allocate request buffer
  410. bsize_t size = bsize_fromsize(sizeof(struct socks_request_header));
  411. switch (o->dest_addr.type) {
  412. case BADDR_TYPE_IPV4:
  413. size = bsize_add(size, bsize_fromsize(sizeof(struct socks_addr_ipv4)));
  414. break;
  415. case BADDR_TYPE_IPV6:
  416. size = bsize_add(size, bsize_fromsize(sizeof(struct socks_addr_ipv6)));
  417. break;
  418. default:
  419. BLog(BLOG_ERROR, "Invalid dest_addr address type.");
  420. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  421. return;
  422. }
  423. if (!reserve_buffer(o, size)) {
  424. report_error(o, BSOCKSCLIENT_EVENT_ERROR);
  425. return;
  426. }
  427. // write request
  428. struct socks_request_header header;
  429. header.ver = hton8(SOCKS_VERSION);
  430. header.cmd = hton8(o->udp ? SOCKS_CMD_UDP_ASSOCIATE : SOCKS_CMD_CONNECT);
  431. header.rsv = hton8(0);
  432. switch (o->dest_addr.type) {
  433. case BADDR_TYPE_IPV4: {
  434. header.atyp = hton8(SOCKS_ATYP_IPV4);
  435. struct socks_addr_ipv4 addr;
  436. addr.addr = o->dest_addr.ipv4.ip;
  437. addr.port = o->dest_addr.ipv4.port;
  438. memcpy(o->buffer + sizeof(header), &addr, sizeof(addr));
  439. } break;
  440. case BADDR_TYPE_IPV6: {
  441. header.atyp = hton8(SOCKS_ATYP_IPV6);
  442. struct socks_addr_ipv6 addr;
  443. memcpy(addr.addr, o->dest_addr.ipv6.ip, sizeof(o->dest_addr.ipv6.ip));
  444. addr.port = o->dest_addr.ipv6.port;
  445. memcpy(o->buffer + sizeof(header), &addr, sizeof(addr));
  446. } break;
  447. default:
  448. ASSERT(0);
  449. }
  450. memcpy(o->buffer, &header, sizeof(header));
  451. // send request
  452. PacketPassInterface_Sender_Send(o->control.send_if, (uint8_t *)o->buffer, size.value);
  453. // set state
  454. o->state = STATE_SENDING_REQUEST;
  455. }
  456. struct BSocksClient_auth_info BSocksClient_auth_none (void)
  457. {
  458. struct BSocksClient_auth_info info;
  459. info.auth_type = SOCKS_METHOD_NO_AUTHENTICATION_REQUIRED;
  460. return info;
  461. }
  462. struct BSocksClient_auth_info BSocksClient_auth_password (const char *username, size_t username_len, const char *password, size_t password_len)
  463. {
  464. struct BSocksClient_auth_info info;
  465. info.auth_type = SOCKS_METHOD_USERNAME_PASSWORD;
  466. info.password.username = username;
  467. info.password.username_len = username_len;
  468. info.password.password = password;
  469. info.password.password_len = password_len;
  470. return info;
  471. }
  472. int BSocksClient_Init (BSocksClient *o, BAddr server_addr,
  473. const struct BSocksClient_auth_info *auth_info, size_t num_auth_info, BAddr dest_addr,
  474. bool udp, BSocksClient_handler handler, void *user, BReactor *reactor)
  475. {
  476. ASSERT(!BAddr_IsInvalid(&server_addr))
  477. #ifndef NDEBUG
  478. for (size_t i = 0; i < num_auth_info; i++) {
  479. ASSERT(auth_info[i].auth_type == SOCKS_METHOD_NO_AUTHENTICATION_REQUIRED ||
  480. auth_info[i].auth_type == SOCKS_METHOD_USERNAME_PASSWORD)
  481. }
  482. #endif
  483. // init arguments
  484. o->auth_info = auth_info;
  485. o->num_auth_info = num_auth_info;
  486. o->dest_addr = dest_addr;
  487. o->udp = udp;
  488. o->handler = handler;
  489. o->user = user;
  490. o->reactor = reactor;
  491. // set no buffer
  492. o->buffer = NULL;
  493. // init continue_job
  494. BPending_Init(&o->continue_job, BReactor_PendingGroup(o->reactor),
  495. (BPending_handler)continue_job_handler, o);
  496. // init connector
  497. if (!BConnector_Init(&o->connector, server_addr, o->reactor, o, (BConnector_handler)connector_handler)) {
  498. BLog(BLOG_ERROR, "BConnector_Init failed");
  499. goto fail0;
  500. }
  501. // set state
  502. o->state = STATE_CONNECTING;
  503. DebugError_Init(&o->d_err, BReactor_PendingGroup(o->reactor));
  504. DebugObject_Init(&o->d_obj);
  505. return 1;
  506. fail0:
  507. BPending_Free(&o->continue_job);
  508. return 0;
  509. }
  510. void BSocksClient_Free (BSocksClient *o)
  511. {
  512. DebugObject_Free(&o->d_obj);
  513. DebugError_Free(&o->d_err);
  514. if (o->state != STATE_CONNECTING) {
  515. if (o->state == STATE_UP) {
  516. // free up I/O
  517. free_up_io(o);
  518. } else {
  519. // free control I/O
  520. free_control_io(o);
  521. }
  522. // free connection
  523. BConnection_Free(&o->con);
  524. }
  525. // free connector
  526. BConnector_Free(&o->connector);
  527. // free continue job
  528. BPending_Free(&o->continue_job);
  529. // free buffer
  530. if (o->buffer) {
  531. BFree(o->buffer);
  532. }
  533. }
  534. int BSocksClient_GetLocalAddr (BSocksClient *o, BAddr *local_addr)
  535. {
  536. ASSERT(o->state != STATE_CONNECTING)
  537. DebugObject_Access(&o->d_obj);
  538. return BConnection_GetLocalAddress(&o->con, local_addr);
  539. }
  540. void BSocksClient_SetDestAddr (BSocksClient *o, BAddr dest_addr)
  541. {
  542. ASSERT(o->state == STATE_CONNECTING || o->state == STATE_CONNECTED_HANDLER)
  543. DebugObject_Access(&o->d_obj);
  544. o->dest_addr = dest_addr;
  545. }
  546. BAddr BSocksClient_GetBindAddr (BSocksClient *o)
  547. {
  548. ASSERT(o->state == STATE_UP)
  549. DebugObject_Access(&o->d_obj);
  550. return o->bind_addr;
  551. }
  552. StreamPassInterface * BSocksClient_GetSendInterface (BSocksClient *o)
  553. {
  554. ASSERT(o->state == STATE_UP)
  555. ASSERT(!o->udp)
  556. DebugObject_Access(&o->d_obj);
  557. return BConnection_SendAsync_GetIf(&o->con);
  558. }
  559. StreamRecvInterface * BSocksClient_GetRecvInterface (BSocksClient *o)
  560. {
  561. ASSERT(o->state == STATE_UP)
  562. ASSERT(!o->udp)
  563. DebugObject_Access(&o->d_obj);
  564. return BConnection_RecvAsync_GetIf(&o->con);
  565. }