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.

734 lines
22 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
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
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
12 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 "local.h"
  21. #include "socks5.h"
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #ifndef MSG_NOSIGNAL
  26. #define MSG_NOSIGNAL MSG_HAVEMORE
  27. #endif
  28. #ifndef EAGAIN
  29. #define EAGAIN EWOULDBLOCK
  30. #endif
  31. #define min(a,b) (((a)<(b))?(a):(b))
  32. static char *_server;
  33. static char *_remote_port;
  34. static int _timeout;
  35. int setnonblocking(int fd) {
  36. int flags;
  37. if (-1 ==(flags = fcntl(fd, F_GETFL, 0)))
  38. flags = 0;
  39. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  40. }
  41. int create_and_bind(const char *port) {
  42. struct addrinfo hints;
  43. struct addrinfo *result, *rp;
  44. int s, listen_sock;
  45. memset(&hints, 0, sizeof(struct addrinfo));
  46. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  47. hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  48. s = getaddrinfo("0.0.0.0", port, &hints, &result);
  49. if (s != 0) {
  50. LOGD("getaddrinfo: %s\n", gai_strerror(s));
  51. return -1;
  52. }
  53. for (rp = result; rp != NULL; rp = rp->ai_next) {
  54. listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  55. if (listen_sock == -1)
  56. continue;
  57. int opt = 1;
  58. int err = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  59. if (err) {
  60. perror("setsocket");
  61. }
  62. s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen);
  63. if (s == 0) {
  64. /* We managed to bind successfully! */
  65. break;
  66. } else {
  67. perror("bind");
  68. }
  69. close(listen_sock);
  70. }
  71. if (rp == NULL) {
  72. LOGE("Could not bind\n");
  73. return -1;
  74. }
  75. freeaddrinfo(result);
  76. return listen_sock;
  77. }
  78. static void server_recv_cb (EV_P_ ev_io *w, int revents) {
  79. struct server_ctx *server_recv_ctx = (struct server_ctx *)w;
  80. struct server *server = server_recv_ctx->server;
  81. struct remote *remote = server->remote;
  82. if (remote == NULL) {
  83. close_and_free_server(EV_A_ server);
  84. return;
  85. }
  86. while (1) {
  87. char *buf = remote->buf;
  88. int *buf_len = &remote->buf_len;
  89. if (server->stage != 5) {
  90. buf = server->buf;
  91. buf_len = &server->buf_len;
  92. }
  93. ssize_t r = recv(server->fd, buf, BUF_SIZE, 0);
  94. if (r == 0) {
  95. // connection closed
  96. *buf_len = 0;
  97. close_and_free_remote(EV_A_ remote);
  98. close_and_free_server(EV_A_ server);
  99. return;
  100. } else if(r < 0) {
  101. if (errno == EAGAIN) {
  102. // no data
  103. // continue to wait for recv
  104. break;
  105. } else {
  106. perror("server recv");
  107. close_and_free_remote(EV_A_ remote);
  108. close_and_free_server(EV_A_ server);
  109. return;
  110. }
  111. }
  112. // local socks5 server
  113. if (server->stage == 5) {
  114. encrypt_ctx(remote->buf, r, server->e_ctx);
  115. int w = send(remote->fd, remote->buf, r, 0);
  116. if(w == -1) {
  117. if (errno == EAGAIN) {
  118. // no data, wait for send
  119. ev_io_stop(EV_A_ &server_recv_ctx->io);
  120. ev_io_start(EV_A_ &remote->send_ctx->io);
  121. break;
  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(w < r) {
  129. char *pt = remote->buf;
  130. char *et = pt + r;
  131. while (pt + w < et) {
  132. *pt = *(pt + w);
  133. pt++;
  134. }
  135. remote->buf_len = r - w;
  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. break;
  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), MSG_NOSIGNAL);
  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\n", 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, MSG_NOSIGNAL);
  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. unsigned char 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. unsigned char name_len = *(unsigned char *)(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++] = *(unsigned char *)(server->buf + 4 + 1 + name_len);
  181. addr_to_send[addr_len++] = *(unsigned char *)(server->buf + 4 + 1 + name_len + 1);
  182. } else {
  183. LOGE("unsupported addrtype: %d\n", 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. memcpy(server->buf + 4, &sin_addr, sizeof(struct in_addr));
  200. *((unsigned short *)(server->buf + 4 + sizeof(struct in_addr)))
  201. = (unsigned short) htons(atoi(_remote_port));
  202. int reply_size = 4 + sizeof(struct in_addr) + sizeof(unsigned short);
  203. int r = send(server->fd, server->buf, reply_size, MSG_NOSIGNAL);
  204. if (r < reply_size) {
  205. LOGE("header not complete sent\n");
  206. close_and_free_remote(EV_A_ remote);
  207. close_and_free_server(EV_A_ server);
  208. return;
  209. }
  210. server->stage = 5;
  211. }
  212. }
  213. }
  214. static void server_send_cb (EV_P_ ev_io *w, int revents) {
  215. struct server_ctx *server_send_ctx = (struct server_ctx *)w;
  216. struct server *server = server_send_ctx->server;
  217. struct remote *remote = server->remote;
  218. if (server->buf_len == 0) {
  219. // close and free
  220. close_and_free_remote(EV_A_ remote);
  221. close_and_free_server(EV_A_ server);
  222. return;
  223. } else {
  224. // has data to send
  225. ssize_t r = send(server->fd, server->buf,
  226. server->buf_len, 0);
  227. if (r < 0) {
  228. if (errno != EAGAIN) {
  229. perror("send");
  230. close_and_free_remote(EV_A_ remote);
  231. close_and_free_server(EV_A_ server);
  232. return;
  233. }
  234. return;
  235. }
  236. if (r < server->buf_len) {
  237. // partly sent, move memory, wait for the next time to send
  238. char *pt = server->buf;
  239. char *et = pt + server->buf_len;
  240. while (pt + r < et) {
  241. *pt = *(pt + r);
  242. pt++;
  243. }
  244. server->buf_len -= r;
  245. assert(server->buf_len >= 0);
  246. return;
  247. } else {
  248. // all sent out, wait for reading
  249. ev_io_stop(EV_A_ &server_send_ctx->io);
  250. if (remote != NULL) {
  251. ev_io_start(EV_A_ &remote->recv_ctx->io);
  252. } else {
  253. close_and_free_remote(EV_A_ remote);
  254. close_and_free_server(EV_A_ server);
  255. return;
  256. }
  257. }
  258. }
  259. }
  260. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents) {
  261. struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
  262. - sizeof(ev_io));
  263. struct remote *remote = remote_ctx->remote;
  264. struct server *server = remote->server;
  265. LOGD("remote timeout\n");
  266. ev_timer_stop(EV_A_ watcher);
  267. if (server == NULL) {
  268. close_and_free_remote(EV_A_ remote);
  269. return;
  270. }
  271. close_and_free_remote(EV_A_ remote);
  272. close_and_free_server(EV_A_ server);
  273. }
  274. static void remote_recv_cb (EV_P_ ev_io *w, int revents) {
  275. struct remote_ctx *remote_recv_ctx = (struct remote_ctx *)w;
  276. struct remote *remote = remote_recv_ctx->remote;
  277. struct server *server = remote->server;
  278. if (server == NULL) {
  279. close_and_free_remote(EV_A_ remote);
  280. return;
  281. }
  282. while (1) {
  283. ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
  284. if (r == 0) {
  285. // connection closed
  286. server->buf_len = 0;
  287. close_and_free_remote(EV_A_ remote);
  288. close_and_free_server(EV_A_ server);
  289. return;
  290. } else if(r < 0) {
  291. if (errno == EAGAIN) {
  292. // no data
  293. // continue to wait for recv
  294. break;
  295. } else {
  296. perror("remote recv");
  297. close_and_free_remote(EV_A_ remote);
  298. close_and_free_server(EV_A_ server);
  299. return;
  300. }
  301. }
  302. decrypt_ctx(server->buf, r, server->d_ctx);
  303. int w = send(server->fd, server->buf, r, MSG_NOSIGNAL);
  304. if(w == -1) {
  305. if (errno == EAGAIN) {
  306. // no data, wait for send
  307. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  308. ev_io_start(EV_A_ &server->send_ctx->io);
  309. break;
  310. } else {
  311. perror("send");
  312. close_and_free_remote(EV_A_ remote);
  313. close_and_free_server(EV_A_ server);
  314. return;
  315. }
  316. } else if(w < r) {
  317. char *pt = server->buf;
  318. char *et = pt + r;
  319. while (pt + w < et) {
  320. *pt = *(pt + w);
  321. pt++;
  322. }
  323. server->buf_len = r - w;
  324. assert(server->buf_len >= 0);
  325. ev_io_stop(EV_A_ &remote_recv_ctx->io);
  326. ev_io_start(EV_A_ &server->send_ctx->io);
  327. break;
  328. }
  329. }
  330. }
  331. static void remote_send_cb (EV_P_ ev_io *w, int revents) {
  332. struct remote_ctx *remote_send_ctx = (struct remote_ctx *)w;
  333. struct remote *remote = remote_send_ctx->remote;
  334. struct server *server = remote->server;
  335. if (!remote_send_ctx->connected) {
  336. struct sockaddr_storage addr;
  337. socklen_t len = sizeof addr;
  338. int r = getpeername(remote->fd, (struct sockaddr*)&addr, &len);
  339. if (r == 0) {
  340. remote_send_ctx->connected = 1;
  341. ev_io_stop(EV_A_ &remote_send_ctx->io);
  342. ev_timer_stop(EV_A_ &remote_send_ctx->watcher);
  343. ev_io_start(EV_A_ &server->recv_ctx->io);
  344. ev_io_start(EV_A_ &remote->recv_ctx->io);
  345. return;
  346. } else {
  347. perror("getpeername");
  348. // not connected
  349. close_and_free_remote(EV_A_ remote);
  350. close_and_free_server(EV_A_ server);
  351. return;
  352. }
  353. } else {
  354. if (remote->buf_len == 0) {
  355. // close and free
  356. close_and_free_remote(EV_A_ remote);
  357. close_and_free_server(EV_A_ server);
  358. return;
  359. } else {
  360. // has data to send
  361. ssize_t r = send(remote->fd, remote->buf,
  362. remote->buf_len, 0);
  363. if (r < 0) {
  364. if (errno != EAGAIN) {
  365. perror("send");
  366. // close and free
  367. close_and_free_remote(EV_A_ remote);
  368. close_and_free_server(EV_A_ server);
  369. return;
  370. }
  371. return;
  372. }
  373. if (r < remote->buf_len) {
  374. // partly sent, move memory, wait for the next time to send
  375. char *pt = remote->buf;
  376. char *et = pt + remote->buf_len;
  377. while (pt + r < et) {
  378. *pt = *(pt + r);
  379. pt++;
  380. }
  381. remote->buf_len -= r;
  382. assert(remote->buf_len >= 0);
  383. return;
  384. } else {
  385. // all sent out, wait for reading
  386. ev_io_stop(EV_A_ &remote_send_ctx->io);
  387. if (server != NULL) {
  388. ev_io_start(EV_A_ &server->recv_ctx->io);
  389. } else {
  390. close_and_free_remote(EV_A_ remote);
  391. close_and_free_server(EV_A_ server);
  392. return;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. struct remote* new_remote(int fd) {
  399. struct remote *remote;
  400. remote = malloc(sizeof(struct remote));
  401. remote->recv_ctx = malloc(sizeof(struct remote_ctx));
  402. remote->send_ctx = malloc(sizeof(struct remote_ctx));
  403. remote->fd = fd;
  404. ev_io_init(&remote->recv_ctx->io, remote_recv_cb, fd, EV_READ);
  405. ev_io_init(&remote->send_ctx->io, remote_send_cb, fd, EV_WRITE);
  406. ev_timer_init(&remote->send_ctx->watcher, remote_timeout_cb, _timeout, 0);
  407. remote->recv_ctx->remote = remote;
  408. remote->recv_ctx->connected = 0;
  409. remote->send_ctx->remote = remote;
  410. remote->send_ctx->connected = 0;
  411. remote->buf_len = 0;
  412. return remote;
  413. }
  414. void free_remote(struct remote *remote) {
  415. if (remote != NULL) {
  416. if (remote->server != NULL) {
  417. remote->server->remote = NULL;
  418. }
  419. free(remote->recv_ctx);
  420. free(remote->send_ctx);
  421. free(remote);
  422. }
  423. }
  424. void close_and_free_remote(EV_P_ struct remote *remote) {
  425. if (remote != NULL) {
  426. ev_timer_stop(EV_A_ &remote->send_ctx->watcher);
  427. ev_io_stop(EV_A_ &remote->send_ctx->io);
  428. ev_io_stop(EV_A_ &remote->recv_ctx->io);
  429. close(remote->fd);
  430. free_remote(remote);
  431. }
  432. }
  433. struct server* new_server(int fd) {
  434. struct server *server;
  435. server = malloc(sizeof(struct server));
  436. server->recv_ctx = malloc(sizeof(struct server_ctx));
  437. server->send_ctx = malloc(sizeof(struct server_ctx));
  438. server->fd = fd;
  439. ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ);
  440. ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE);
  441. server->recv_ctx->server = server;
  442. server->recv_ctx->connected = 0;
  443. server->send_ctx->server = server;
  444. server->send_ctx->connected = 0;
  445. server->stage = 0;
  446. if (_method == RC4) {
  447. server->e_ctx = malloc(sizeof(EVP_CIPHER_CTX));
  448. server->d_ctx = malloc(sizeof(EVP_CIPHER_CTX));
  449. enc_ctx_init(server->e_ctx, 1);
  450. enc_ctx_init(server->d_ctx, 0);
  451. } else {
  452. server->e_ctx = NULL;
  453. server->d_ctx = NULL;
  454. }
  455. server->buf_len = 0;
  456. return server;
  457. }
  458. void free_server(struct server *server) {
  459. if (server != NULL) {
  460. if (server->remote != NULL) {
  461. server->remote->server = NULL;
  462. }
  463. if (_method == RC4) {
  464. EVP_CIPHER_CTX_cleanup(server->e_ctx);
  465. EVP_CIPHER_CTX_cleanup(server->d_ctx);
  466. free(server->e_ctx);
  467. free(server->d_ctx);
  468. }
  469. free(server->recv_ctx);
  470. free(server->send_ctx);
  471. free(server);
  472. }
  473. }
  474. void close_and_free_server(EV_P_ struct server *server) {
  475. if (server != NULL) {
  476. ev_io_stop(EV_A_ &server->send_ctx->io);
  477. ev_io_stop(EV_A_ &server->recv_ctx->io);
  478. close(server->fd);
  479. free_server(server);
  480. }
  481. }
  482. static void accept_cb (EV_P_ ev_io *w, int revents)
  483. {
  484. struct listen_ctx *listener = (struct listen_ctx *)w;
  485. int serverfd;
  486. while (1) {
  487. serverfd = accept(listener->fd, NULL, NULL);
  488. if (serverfd == -1) {
  489. perror("accept");
  490. break;
  491. }
  492. setnonblocking(serverfd);
  493. struct server *server = new_server(serverfd);
  494. struct addrinfo hints, *res;
  495. int sockfd;
  496. memset(&hints, 0, sizeof hints);
  497. hints.ai_family = AF_UNSPEC;
  498. hints.ai_socktype = SOCK_STREAM;
  499. int err = getaddrinfo(_server, _remote_port, &hints, &res);
  500. if (err) {
  501. perror("getaddrinfo");
  502. close_and_free_server(EV_A_ server);
  503. break;
  504. }
  505. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  506. if (sockfd < 0) {
  507. perror("socket");
  508. close(sockfd);
  509. close_and_free_server(EV_A_ server);
  510. freeaddrinfo(res);
  511. break;
  512. }
  513. struct timeval timeout;
  514. timeout.tv_sec = _timeout;
  515. timeout.tv_usec = 0;
  516. err = setsockopt(sockfd, SOL_SOCKET,
  517. SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  518. if (err) perror("setsockopt");
  519. err = setsockopt(sockfd, SOL_SOCKET,
  520. SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  521. if (err) perror("setsockopt");
  522. setnonblocking(sockfd);
  523. struct remote *remote = new_remote(sockfd);
  524. server->remote = remote;
  525. remote->server = server;
  526. connect(sockfd, res->ai_addr, res->ai_addrlen);
  527. freeaddrinfo(res);
  528. // listen to remote connected event
  529. ev_io_start(EV_A_ &remote->send_ctx->io);
  530. ev_timer_start(EV_A_ &remote->send_ctx->watcher);
  531. break;
  532. }
  533. }
  534. static void print_usage() {
  535. printf("usage: ss -s server_host -p server_port -l local_port\n");
  536. printf(" -k password [-m encrypt_method] [-f pid_file]\n");
  537. printf("\n");
  538. printf("info:\n");
  539. printf(" encrypt_method: table, rc4\n");
  540. printf(" pid_file: valid path to the pid file\n");
  541. }
  542. int main (int argc, char **argv)
  543. {
  544. char *server = NULL;
  545. char *remote_port = NULL;
  546. char *port = NULL;
  547. char *key = NULL;
  548. char *timeout = "10";
  549. char *method = NULL;
  550. int c;
  551. int f_flags = 0;
  552. char *f_path = NULL;
  553. opterr = 0;
  554. while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:")) != -1) {
  555. switch (c) {
  556. case 's':
  557. server = optarg;
  558. break;
  559. case 'p':
  560. remote_port = optarg;
  561. break;
  562. case 'l':
  563. port = optarg;
  564. break;
  565. case 'k':
  566. key = optarg;
  567. break;
  568. case 'f':
  569. f_flags = 1;
  570. f_path = optarg;
  571. break;
  572. case 't':
  573. timeout = optarg;
  574. break;
  575. case 'm':
  576. method = optarg;
  577. break;
  578. }
  579. }
  580. if (server == NULL || remote_port == NULL ||
  581. port == NULL || key == NULL) {
  582. print_usage();
  583. exit(EXIT_FAILURE);
  584. }
  585. if (f_flags) {
  586. if (f_path == NULL) {
  587. print_usage();
  588. exit(EXIT_FAILURE);
  589. }
  590. /* Our process ID and Session ID */
  591. pid_t pid, sid;
  592. /* Fork off the parent process */
  593. pid = fork();
  594. if (pid < 0) {
  595. exit(EXIT_FAILURE);
  596. }
  597. /* If we got a good PID, then
  598. we can exit the parent process. */
  599. if (pid > 0) {
  600. FILE *file = fopen(f_path, "w");
  601. fprintf(file, "%d", pid);
  602. fclose(file);
  603. exit(EXIT_SUCCESS);
  604. }
  605. /* Change the file mode mask */
  606. umask(0);
  607. /* Open any logs here */
  608. /* Create a new SID for the child process */
  609. sid = setsid();
  610. if (sid < 0) {
  611. /* Log the failure */
  612. exit(EXIT_FAILURE);
  613. }
  614. /* Change the current working directory */
  615. if ((chdir("/")) < 0) {
  616. /* Log the failure */
  617. exit(EXIT_FAILURE);
  618. }
  619. /* Close out the standard file descriptors */
  620. close(STDIN_FILENO);
  621. close(STDOUT_FILENO);
  622. close(STDERR_FILENO);
  623. }
  624. // init global variables
  625. _server = strdup(server);
  626. _remote_port = strdup(remote_port);
  627. _timeout = atoi(timeout);
  628. _method = TABLE;
  629. if (method != NULL) {
  630. if (strcmp(method, "rc4") == 0) {
  631. _method = RC4;
  632. }
  633. }
  634. LOGD("calculating ciphers %d\n", _method);
  635. if (_method == RC4) {
  636. enc_key_init(key);
  637. } else {
  638. get_table(key);
  639. }
  640. int listenfd;
  641. listenfd = create_and_bind(port);
  642. if (listenfd < 0) {
  643. LOGE("bind() error..\n");
  644. return 1;
  645. }
  646. if (listen(listenfd, SOMAXCONN) == -1) {
  647. LOGE("listen() error.\n");
  648. return 1;
  649. }
  650. LOGD("server listening at port %s\n", port);
  651. setnonblocking(listenfd);
  652. struct listen_ctx listen_ctx;
  653. listen_ctx.fd = listenfd;
  654. struct ev_loop *loop = ev_default_loop(0);
  655. if (!loop) {
  656. return 1;
  657. }
  658. ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ);
  659. ev_io_start (loop, &listen_ctx.io);
  660. ev_run (loop, 0);
  661. return 0;
  662. }