You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

700 lines
20 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
11 years ago
11 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
11 years ago
11 years ago
12 years ago
  1. #include <sys/socket.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <arpa/inet.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <locale.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <netinet/tcp.h>
  11. #include <pthread.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <strings.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <assert.h>
  20. #include "utils.h"
  21. #include "local.h"
  22. #include "socks5.h"
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #ifndef EAGAIN
  27. #define EAGAIN EWOULDBLOCK
  28. #endif
  29. #ifndef EWOULDBLOCK
  30. #define EWOULDBLOCK EAGAIN
  31. #endif
  32. #define min(a,b) (((a)<(b))?(a):(b))
  33. int setnonblocking(int fd) {
  34. int flags;
  35. if (-1 ==(flags = fcntl(fd, F_GETFL, 0)))
  36. flags = 0;
  37. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  38. }
  39. int create_and_bind(const char *port) {
  40. struct addrinfo hints;
  41. struct addrinfo *result, *rp;
  42. int s, listen_sock;
  43. memset(&hints, 0, sizeof(struct addrinfo));
  44. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  45. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  46. s = getaddrinfo("0.0.0.0", port, &hints, &result);
  47. if (s != 0) {
  48. LOGD("getaddrinfo: %s", gai_strerror(s));
  49. return -1;
  50. }
  51. for (rp = result; rp != NULL; rp = rp->ai_next) {
  52. listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  53. if (listen_sock == -1)
  54. continue;
  55. int opt = 1;
  56. int err = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  57. if (err) {
  58. perror("setsocket");
  59. }
  60. s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
  61. if (s == 0) {
  62. /* We managed to bind successfully! */
  63. break;
  64. } else {
  65. perror("bind");
  66. }
  67. close(listen_sock);
  68. }
  69. if (rp == NULL) {
  70. LOGE("Could not bind");
  71. return -1;
  72. }
  73. freeaddrinfo(result);
  74. return listen_sock;
  75. }
  76. static void server_recv_cb (EV_P_ ev_io *w, int revents) {
  77. struct server_ctx *server_recv_ctx = (struct server_ctx *)w;
  78. struct server *server = server_recv_ctx->server;
  79. struct remote *remote = server->remote;
  80. if (remote == NULL) {
  81. close_and_free_server(EV_A_ server);
  82. return;
  83. }
  84. char *buf = remote->buf;
  85. int *buf_len = &remote->buf_len;
  86. if (server->stage != 5) {
  87. buf = server->buf;
  88. buf_len = &server->buf_len;
  89. }
  90. ssize_t r = recv(server->fd, buf, BUF_SIZE, 0);
  91. if (r == 0) {
  92. // connection closed
  93. *buf_len = 0;
  94. close_and_free_server(EV_A_ server);
  95. if (remote != NULL) {
  96. ev_io_start(EV_A_ &remote->send_ctx->io);
  97. }
  98. return;
  99. } else if(r < 0) {
  100. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  101. // no data
  102. // continue to wait for recv
  103. return;
  104. } else {
  105. perror("server recv");
  106. close_and_free_remote(EV_A_ remote);
  107. close_and_free_server(EV_A_ server);
  108. return;
  109. }
  110. }
  111. // local socks5 server
  112. if (server->stage == 5) {
  113. encrypt_ctx(remote->buf, r, server->e_ctx);
  114. int s = send(remote->fd, remote->buf, r, 0);
  115. if(s == -1) {
  116. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  117. // no data, wait for send
  118. remote->buf_len = r;
  119. ev_io_stop(EV_A_ &server_recv_ctx->io);
  120. ev_io_start(EV_A_ &remote->send_ctx->io);
  121. return;
  122. } else {
  123. perror("send");
  124. close_and_free_remote(EV_A_ remote);
  125. close_and_free_server(EV_A_ server);
  126. return;
  127. }
  128. } else if(s < r) {
  129. char *pt = remote->buf;
  130. char *et = pt + s;
  131. while (pt + s < et) {
  132. *pt = *(pt + s);
  133. pt++;
  134. }
  135. remote->buf_len = r - s;
  136. assert(remote->buf_len >= 0);
  137. ev_io_stop(EV_A_ &server_recv_ctx->io);
  138. ev_io_start(EV_A_ &remote->send_ctx->io);
  139. return;
  140. }
  141. } else if (server->stage == 0) {
  142. struct method_select_response response;
  143. response.ver = SVERSION;
  144. response.method = 0;
  145. char *send_buf = (char *)&response;
  146. send(server->fd, send_buf, sizeof(response), 0);
  147. server->stage = 1;
  148. return;
  149. } else if (server->stage == 1) {
  150. struct socks5_request *request = (struct socks5_request *)server->buf;
  151. if (request->cmd != 1) {
  152. LOGE("unsupported cmd: %d", request->cmd);
  153. struct socks5_response response;
  154. response.ver = SVERSION;
  155. response.rep = CMD_NOT_SUPPORTED;
  156. response.rsv = 0;
  157. response.atyp = 1;
  158. char *send_buf = (char *)&response;
  159. send(server->fd, send_buf, 4, 0);
  160. close_and_free_remote(EV_A_ remote);
  161. close_and_free_server(EV_A_ server);
  162. return;
  163. }
  164. char addr_to_send[256];
  165. uint8_t addr_len = 0;
  166. addr_to_send[addr_len++] = request->atyp;
  167. // get remote addr and port
  168. if (request->atyp == 1) {
  169. // IP V4
  170. size_t in_addr_len = sizeof(struct in_addr);
  171. memcpy(addr_to_send + addr_len, server->buf + 4, in_addr_len + 2);
  172. addr_len += in_addr_len + 2;
  173. } else if (request->atyp == 3) {
  174. // Domain name
  175. uint8_t name_len = *(uint8_t *)(server->buf + 4);
  176. addr_to_send[addr_len++] = name_len;
  177. memcpy(addr_to_send + addr_len, server->buf + 4 + 1, name_len);
  178. addr_len += name_len;
  179. // get port
  180. addr_to_send[addr_len++] = *(uint8_t *)(server->buf + 4 + 1 + name_len);
  181. addr_to_send[addr_len++] = *(uint8_t *)(server->buf + 4 + 1 + name_len + 1);
  182. } else {
  183. LOGE("unsupported addrtype: %d", request->atyp);
  184. close_and_free_remote(EV_A_ remote);
  185. close_and_free_server(EV_A_ server);
  186. return;
  187. }
  188. encrypt_ctx(addr_to_send, addr_len, server->e_ctx);
  189. send(remote->fd, addr_to_send, addr_len, 0);
  190. // Fake reply
  191. struct socks5_response response;
  192. response.ver = SVERSION;
  193. response.rep = 0;
  194. response.rsv = 0;
  195. response.atyp = 1;
  196. struct in_addr sin_addr;
  197. inet_aton("0.0.0.0", &sin_addr);
  198. memcpy(server->buf, &response, 4);
  199. memset(server->buf + 4, 0, sizeof(struct in_addr) + sizeof(uint16_t));
  200. int reply_size = 4 + sizeof(struct in_addr) + sizeof(uint16_t);
  201. int s = send(server->fd, server->buf, reply_size, 0);
  202. if (s < reply_size) {
  203. LOGE("header not complete sent");
  204. close_and_free_remote(EV_A_ remote);
  205. close_and_free_server(EV_A_ server);
  206. return;
  207. }
  208. server->stage = 5;
  209. }
  210. }
  211. static void server_send_cb (EV_P_ ev_io *w, int revents) {
  212. struct server_ctx *server_send_ctx = (struct server_ctx *)w;
  213. struct server *server = server_send_ctx->server;
  214. struct remote *remote = server->remote;
  215. if (server->buf_len == 0) {
  216. // close and free
  217. close_and_free_remote(EV_A_ remote);
  218. close_and_free_server(EV_A_ server);
  219. return;
  220. } else {
  221. // has data to send
  222. ssize_t s = send(server->fd, server->buf,
  223. server->buf_len, 0);
  224. if (s < 0) {
  225. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  226. perror("send");
  227. close_and_free_remote(EV_A_ remote);
  228. close_and_free_server(EV_A_ server);
  229. }
  230. return;
  231. } else if (s < server->buf_len) {
  232. // partly sent, move memory, wait for the next time to send
  233. char *pt = server->buf;
  234. char *et = pt + server->buf_len;
  235. while (pt + s < et) {
  236. *pt = *(pt + s);
  237. pt++;
  238. }
  239. server->buf_len -= s;
  240. assert(server->buf_len >= 0);
  241. return;
  242. } else {
  243. // all sent out, wait for reading
  244. ev_io_stop(EV_A_ &server_send_ctx->io);
  245. if (remote != NULL) {
  246. ev_io_start(EV_A_ &remote->recv_ctx->io);
  247. } else {
  248. close_and_free_remote(EV_A_ remote);
  249. close_and_free_server(EV_A_ server);
  250. return;
  251. }
  252. }
  253. }
  254. }
  255. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) {
  256. struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
  257. - sizeof(ev_io));
  258. struct remote *remote = remote_ctx->remote;
  259. struct server *server = remote->server;
  260. LOGD("remote timeout");
  261. ev_timer_stop(EV_A_ watcher);
  262. if (server == NULL) {
  263. close_and_free_remote(EV_A_ remote);
  264. return;
  265. }
  266. close_and_free_remote(EV_A_ remote);
  267. close_and_free_server(EV_A_ server);
  268. }
  269. static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
  270. struct remote_ctx *remote_recv_ctx = (struct remote_ctx *)w;
  271. struct remote *remote = remote_recv_ctx->remote;
  272. struct server *server = remote->server;
  273. if (server == NULL) {
  274. close_and_free_remote(EV_A_ remote);
  275. return;
  276. }
  277. ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
  278. if (r == 0) {
  279. // connection closed
  280. server->buf_len = 0;
  281. close_and_free_remote(EV_A_ remote);
  282. if (server != NULL) {
  283. ev_io_start(EV_A_ &server->send_ctx->io);
  284. }
  285. return;
  286. } else if(r < 0) {
  287. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  288. // no data
  289. // continue to wait for recv
  290. return;
  291. } else {
  292. perror("remote recv");
  293. close_and_free_remote(EV_A_ remote);
  294. close_and_free_server(EV_A_ server);
  295. return;
  296. }
  297. }
  298. decrypt_ctx(server->buf, r, server->d_ctx);
  299. int s = send(server->fd, server->buf, r, 0);
  300. if (s == -1) {
  301. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  302. // no data, wait for send
  303. server->buf_len = r;
  304. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  305. ev_io_start(EV_A_ &server->send_ctx->io);
  306. return;
  307. } else {
  308. perror("send");
  309. close_and_free_remote(EV_A_ remote);
  310. close_and_free_server(EV_A_ server);
  311. return;
  312. }
  313. } else if (s < r) {
  314. char *pt = server->buf;
  315. char *et = pt + r;
  316. while (pt + s < et) {
  317. *pt = *(pt + s);
  318. pt++;
  319. }
  320. server->buf_len = r - s;
  321. assert(server->buf_len >= 0);
  322. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  323. ev_io_start(EV_A_ &server->send_ctx->io);
  324. return;
  325. }
  326. }
  327. static void remote_send_cb (EV_P_ ev_io *w, int revents) {
  328. struct remote_ctx *remote_send_ctx = (struct remote_ctx *)w;
  329. struct remote *remote = remote_send_ctx->remote;
  330. struct server *server = remote->server;
  331. if (!remote_send_ctx->connected) {
  332. struct sockaddr_storage addr;
  333. socklen_t len = sizeof addr;
  334. int r = getpeername(remote->fd, (struct sockaddr*)&addr, &len);
  335. if (r == 0) {
  336. remote_send_ctx->connected = 1;
  337. ev_io_stop(EV_A_ &remote_send_ctx->io);
  338. ev_timer_stop(EV_A_ &remote_send_ctx->watcher);
  339. ev_io_start(EV_A_ &server->recv_ctx->io);
  340. ev_io_start(EV_A_ &remote->recv_ctx->io);
  341. return;
  342. } else {
  343. perror("getpeername");
  344. // not connected
  345. close_and_free_remote(EV_A_ remote);
  346. close_and_free_server(EV_A_ server);
  347. return;
  348. }
  349. } else {
  350. if (remote->buf_len == 0) {
  351. // close and free
  352. close_and_free_remote(EV_A_ remote);
  353. close_and_free_server(EV_A_ server);
  354. return;
  355. } else {
  356. // has data to send
  357. ssize_t s = send(remote->fd, remote->buf,
  358. remote->buf_len, 0);
  359. if (s < 0) {
  360. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  361. perror("send");
  362. // close and free
  363. close_and_free_remote(EV_A_ remote);
  364. close_and_free_server(EV_A_ server);
  365. }
  366. return;
  367. } else if (s < remote->buf_len) {
  368. // partly sent, move memory, wait for the next time to send
  369. char *pt = remote->buf;
  370. char *et = pt + remote->buf_len;
  371. while (pt + s < et) {
  372. *pt = *(pt + s);
  373. pt++;
  374. }
  375. remote->buf_len -= s;
  376. assert(remote->buf_len >= 0);
  377. return;
  378. } else {
  379. // all sent out, wait for reading
  380. ev_io_stop(EV_A_ &remote_send_ctx->io);
  381. if (server != NULL) {
  382. ev_io_start(EV_A_ &server->recv_ctx->io);
  383. } else {
  384. close_and_free_remote(EV_A_ remote);
  385. close_and_free_server(EV_A_ server);
  386. return;
  387. }
  388. }
  389. }
  390. }
  391. }
  392. struct remote* new_remote(int fd, int timeout) {
  393. struct remote *remote;
  394. remote = malloc(sizeof(struct remote));
  395. remote->recv_ctx = malloc(sizeof(struct remote_ctx));
  396. remote->send_ctx = malloc(sizeof(struct remote_ctx));
  397. remote->fd = fd;
  398. ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
  399. ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
  400. ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, timeout, 0);
  401. remote->recv_ctx->remote = remote;
  402. remote->recv_ctx->connected = 0;
  403. remote->send_ctx->remote = remote;
  404. remote->send_ctx->connected = 0;
  405. remote->buf_len = 0;
  406. return remote;
  407. }
  408. void free_remote(struct remote *remote) {
  409. if (remote != NULL) {
  410. if (remote->server != NULL) {
  411. remote->server->remote = NULL;
  412. }
  413. free(remote->recv_ctx);
  414. free(remote->send_ctx);
  415. free(remote);
  416. }
  417. }
  418. void close_and_free_remote(EV_P_ struct remote *remote) {
  419. if (remote != NULL) {
  420. ev_timer_stop(EV_A_ &remote->send_ctx->watcher);
  421. ev_io_stop(EV_A_ &remote->send_ctx->io);
  422. ev_io_stop(EV_A_ &remote->recv_ctx->io);
  423. close(remote->fd);
  424. free_remote(remote);
  425. }
  426. }
  427. struct server* new_server(int fd) {
  428. struct server *server;
  429. server = malloc(sizeof(struct server));
  430. server->recv_ctx = malloc(sizeof(struct server_ctx));
  431. server->send_ctx = malloc(sizeof(struct server_ctx));
  432. server->fd = fd;
  433. ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
  434. ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
  435. server->recv_ctx->server = server;
  436. server->recv_ctx->connected = 0;
  437. server->send_ctx->server = server;
  438. server->send_ctx->connected = 0;
  439. server->stage = 0;
  440. if (enc_conf.method == RC4) {
  441. server->e_ctx = malloc(sizeof(struct rc4_state));
  442. server->d_ctx = malloc(sizeof(struct rc4_state));
  443. enc_ctx_init(server->e_ctx, 1);
  444. enc_ctx_init(server->d_ctx, 0);
  445. } else {
  446. server->e_ctx = NULL;
  447. server->d_ctx = NULL;
  448. }
  449. server->buf_len = 0;
  450. return server;
  451. }
  452. void free_server(struct server *server) {
  453. if (server != NULL) {
  454. if (server->remote != NULL) {
  455. server->remote->server = NULL;
  456. }
  457. if (enc_conf.method == RC4) {
  458. free(server->e_ctx);
  459. free(server->d_ctx);
  460. }
  461. free(server->recv_ctx);
  462. free(server->send_ctx);
  463. free(server);
  464. }
  465. }
  466. void close_and_free_server(EV_P_ struct server *server) {
  467. if (server != NULL) {
  468. ev_io_stop(EV_A_ &server->send_ctx->io);
  469. ev_io_stop(EV_A_ &server->recv_ctx->io);
  470. close(server->fd);
  471. free_server(server);
  472. }
  473. }
  474. static void accept_cb (EV_P_ ev_io *w, int revents) {
  475. struct listen_ctx *listener = (struct listen_ctx *)w;
  476. int serverfd = accept(listener->fd, NULL, NULL);
  477. if (serverfd == -1) {
  478. perror("accept");
  479. return;
  480. }
  481. setnonblocking(serverfd);
  482. struct addrinfo hints, *res;
  483. int sockfd;
  484. memset(&hints, 0, sizeof hints);
  485. hints.ai_family = AF_UNSPEC;
  486. hints.ai_socktype = SOCK_STREAM;
  487. int index = clock() % listener->remote_num;
  488. int err = getaddrinfo(listener->remote_host[index], listener->remote_port, &hints, &res);
  489. if (err) {
  490. perror("getaddrinfo");
  491. return;
  492. }
  493. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  494. if (sockfd < 0) {
  495. perror("socket");
  496. close(sockfd);
  497. freeaddrinfo(res);
  498. return;
  499. }
  500. struct timeval timeout;
  501. timeout.tv_sec = listener->timeout;
  502. timeout.tv_usec = 0;
  503. err = setsockopt(sockfd, SOL_SOCKET,
  504. SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  505. if (err) perror("setsockopt");
  506. err = setsockopt(sockfd, SOL_SOCKET,
  507. SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  508. if (err) perror("setsockopt");
  509. setnonblocking(sockfd);
  510. struct server *server = new_server(serverfd);
  511. struct remote *remote = new_remote(sockfd, listener->timeout);
  512. server->remote = remote;
  513. remote->server = server;
  514. connect(sockfd, res->ai_addr, res->ai_addrlen);
  515. freeaddrinfo(res);
  516. // listen to remote connected event
  517. ev_io_start(EV_A_ &remote->send_ctx->io);
  518. ev_timer_start(EV_A_ &remote->send_ctx->watcher);
  519. }
  520. int main (int argc, char **argv) {
  521. int i, c;
  522. int pid_flags = 0;
  523. char *local_port = NULL;
  524. char *password = NULL;
  525. char *timeout = NULL;
  526. char *method = NULL;
  527. char *pid_path = NULL;
  528. char *conf_path = NULL;
  529. int remote_num = 0;
  530. char *remote_host[MAX_REMOTE_NUM];
  531. char *remote_port = NULL;
  532. opterr = 0;
  533. while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:")) != -1) {
  534. switch (c) {
  535. case 's':
  536. remote_host[remote_num++] = optarg;
  537. break;
  538. case 'p':
  539. remote_port = optarg;
  540. break;
  541. case 'l':
  542. local_port = optarg;
  543. break;
  544. case 'k':
  545. password = optarg;
  546. break;
  547. case 'f':
  548. pid_flags = 1;
  549. pid_path = optarg;
  550. break;
  551. case 't':
  552. timeout = optarg;
  553. break;
  554. case 'm':
  555. method = optarg;
  556. break;
  557. case 'c':
  558. conf_path = optarg;
  559. break;
  560. }
  561. }
  562. if (opterr) {
  563. usage();
  564. exit(EXIT_FAILURE);
  565. }
  566. if (conf_path != NULL) {
  567. jconf_t *conf = read_jconf(conf_path);
  568. if (remote_num == 0) {
  569. remote_num = conf->remote_num;
  570. for (i = 0; i < remote_num; i++) {
  571. remote_host[i] = conf->remote_host[i];
  572. }
  573. }
  574. if (remote_port == NULL) remote_port = conf->remote_port;
  575. if (local_port == NULL) local_port = conf->local_port;
  576. if (password == NULL) password = conf->password;
  577. if (method == NULL) method = conf->method;
  578. if (timeout == NULL) timeout = conf->timeout;
  579. }
  580. if (remote_num == 0 || remote_port == NULL ||
  581. local_port == NULL || password == NULL) {
  582. usage();
  583. exit(EXIT_FAILURE);
  584. }
  585. if (timeout == NULL) timeout = "10";
  586. if (pid_flags) {
  587. demonize(pid_path);
  588. }
  589. // ignore SIGPIPE
  590. signal(SIGPIPE, SIG_IGN);
  591. // Setup keys
  592. LOGD("calculating ciphers...");
  593. enc_conf_init(password, method);
  594. // Setup socket
  595. int listenfd;
  596. listenfd = create_and_bind(local_port);
  597. if (listenfd < 0) {
  598. FATAL("bind() error..");
  599. }
  600. if (listen(listenfd, SOMAXCONN) == -1) {
  601. FATAL("listen() error.");
  602. }
  603. setnonblocking(listenfd);
  604. LOGD("server listening at port %s.", local_port);
  605. // Setup proxy context
  606. struct listen_ctx listen_ctx;
  607. listen_ctx.remote_num = remote_num;
  608. listen_ctx.remote_host = malloc(sizeof(char *) * remote_num);
  609. while (remote_num > 0) {
  610. int index = --remote_num;
  611. listen_ctx.remote_host[index] = strdup(remote_host[index]);
  612. }
  613. listen_ctx.remote_port = strdup(remote_port);
  614. listen_ctx.timeout = atoi(timeout);
  615. listen_ctx.fd = listenfd;
  616. struct ev_loop *loop = ev_default_loop(0);
  617. if (!loop) {
  618. FATAL("ev_loop error.");
  619. }
  620. ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
  621. ev_io_start (loop, &listen_ctx.io);
  622. ev_run (loop, 0);
  623. return 0;
  624. }