nio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #include "iowatcher.h"
  2. #ifndef EVENT_IOCP
  3. #include "hio.h"
  4. #include "hsocket.h"
  5. #include "hlog.h"
  6. #ifdef WITH_OPENSSL
  7. #include "openssl/ssl.h"
  8. #include "openssl/err.h"
  9. #include "ssl_ctx.h"
  10. static void ssl_do_handshark(hio_t* io) {
  11. SSL* ssl = (SSL*)io->ssl;
  12. printd("ssl handshark...\n");
  13. int ret = SSL_do_handshake(ssl);
  14. if (ret == 1) {
  15. // handshark finish
  16. iowatcher_del_event(io->loop, io->fd, READ_EVENT);
  17. io->events &= ~READ_EVENT;
  18. io->cb = NULL;
  19. printd("ssl handshark finished.\n");
  20. if (io->accept_cb) {
  21. io->accept_cb(io);
  22. }
  23. else if (io->connect_cb) {
  24. io->connect_cb(io);
  25. }
  26. }
  27. else {
  28. int errcode = SSL_get_error(ssl, ret);
  29. if (errcode == SSL_ERROR_WANT_READ) {
  30. if ((io->events & READ_EVENT) == 0) {
  31. hio_add(io, ssl_do_handshark, READ_EVENT);
  32. }
  33. }
  34. else {
  35. hloge("ssl handshake failed: %d", errcode);
  36. hclose(io);
  37. }
  38. }
  39. }
  40. #endif
  41. static void nio_accept(hio_t* io) {
  42. //printd("nio_accept listenfd=%d\n", io->fd);
  43. socklen_t addrlen;
  44. accept:
  45. addrlen = sizeof(struct sockaddr_in6);
  46. int connfd = accept(io->fd, io->peeraddr, &addrlen);
  47. hio_t* connio = NULL;
  48. if (connfd < 0) {
  49. if (socket_errno() == EAGAIN) {
  50. //goto accept_done;
  51. return;
  52. }
  53. else {
  54. io->error = socket_errno();
  55. perror("accept");
  56. goto accept_error;
  57. }
  58. }
  59. addrlen = sizeof(struct sockaddr_in6);
  60. getsockname(connfd, io->localaddr, &addrlen);
  61. connio = hio_get(io->loop, connfd);
  62. #ifdef WITH_OPENSSL
  63. if (io->io_type == HIO_TYPE_SSL) {
  64. SSL_CTX* ssl_ctx = (SSL_CTX*)g_ssl_ctx;
  65. if (ssl_ctx == NULL) {
  66. goto accept_error;
  67. }
  68. SSL* ssl = SSL_new(ssl_ctx);
  69. SSL_set_fd(ssl, connfd);
  70. connio->ssl = ssl;
  71. connio->accept_cb = io->accept_cb;
  72. connio->userdata = io->userdata;
  73. hio_enable_ssl(connio);
  74. //int ret = SSL_accept(ssl);
  75. SSL_set_accept_state(ssl);
  76. ssl_do_handshark(connio);
  77. }
  78. #endif
  79. if (io->io_type != HIO_TYPE_SSL) {
  80. // NOTE: SSL call accept_cb after handshark finished
  81. if (io->accept_cb) {
  82. char localaddrstr[INET6_ADDRSTRLEN+16] = {0};
  83. char peeraddrstr[INET6_ADDRSTRLEN+16] = {0};
  84. printd("accept listenfd=%d connfd=%d [%s] <= [%s]\n", io->fd, connfd,
  85. sockaddr_snprintf(io->localaddr, localaddrstr, sizeof(localaddrstr)),
  86. sockaddr_snprintf(io->peeraddr, peeraddrstr, sizeof(peeraddrstr)));
  87. //printd("accept_cb------\n");
  88. io->accept_cb(connio);
  89. //printd("accept_cb======\n");
  90. }
  91. }
  92. goto accept;
  93. accept_error:
  94. hclose(io);
  95. }
  96. static void nio_connect(hio_t* io) {
  97. //printd("nio_connect connfd=%d\n", io->fd);
  98. socklen_t addrlen = sizeof(struct sockaddr_in6);
  99. int ret = getpeername(io->fd, io->peeraddr, &addrlen);
  100. if (ret < 0) {
  101. io->error = socket_errno();
  102. printd("connect failed: %s: %d\n", strerror(socket_errno()), socket_errno());
  103. goto connect_failed;
  104. }
  105. else {
  106. addrlen = sizeof(struct sockaddr_in6);
  107. getsockname(io->fd, io->localaddr, &addrlen);
  108. char localaddrstr[INET6_ADDRSTRLEN+16] = {0};
  109. char peeraddrstr[INET6_ADDRSTRLEN+16] = {0};
  110. printd("connect connfd=%d [%s] => [%s]\n", io->fd,
  111. sockaddr_snprintf(io->localaddr, localaddrstr, sizeof(localaddrstr)),
  112. sockaddr_snprintf(io->peeraddr, peeraddrstr, sizeof(peeraddrstr)));
  113. #ifdef WITH_OPENSSL
  114. if (io->io_type == HIO_TYPE_SSL) {
  115. SSL_CTX* ssl_ctx = (SSL_CTX*)g_ssl_ctx;
  116. if (ssl_ctx == NULL) {
  117. goto connect_failed;
  118. }
  119. SSL* ssl = SSL_new(ssl_ctx);
  120. SSL_set_fd(ssl, io->fd);
  121. io->ssl = ssl;
  122. //int ret = SSL_connect(ssl);
  123. SSL_set_connect_state(ssl);
  124. ssl_do_handshark(io);
  125. }
  126. #endif
  127. if (io->io_type != HIO_TYPE_SSL) {
  128. // NOTE: SSL call connect_cb after handshark finished
  129. if (io->connect_cb) {
  130. //printd("connect_cb------\n");
  131. io->connect_cb(io);
  132. //printd("connect_cb======\n");
  133. }
  134. }
  135. return;
  136. }
  137. connect_failed:
  138. hclose(io);
  139. }
  140. static void nio_read(hio_t* io) {
  141. //printd("nio_read fd=%d\n", io->fd);
  142. int nread;
  143. void* buf = io->readbuf.base;
  144. int len = io->readbuf.len;
  145. read:
  146. memset(buf, 0, len);
  147. switch (io->io_type) {
  148. #ifdef WITH_OPENSSL
  149. case HIO_TYPE_SSL:
  150. nread = SSL_read((SSL*)io->ssl, buf, len);
  151. break;
  152. #endif
  153. case HIO_TYPE_TCP:
  154. #ifdef OS_UNIX
  155. nread = read(io->fd, buf, len);
  156. #else
  157. nread = recv(io->fd, buf, len, 0);
  158. #endif
  159. break;
  160. case HIO_TYPE_UDP:
  161. case HIO_TYPE_IP:
  162. {
  163. socklen_t addrlen = sizeof(struct sockaddr_in6);
  164. nread = recvfrom(io->fd, buf, len, 0, io->peeraddr, &addrlen);
  165. }
  166. break;
  167. default:
  168. nread = read(io->fd, buf, len);
  169. break;
  170. }
  171. //printd("read retval=%d\n", nread);
  172. if (nread < 0) {
  173. if (socket_errno() == EAGAIN) {
  174. //goto read_done;
  175. return;
  176. }
  177. else {
  178. io->error = socket_errno();
  179. perror("read");
  180. goto read_error;
  181. }
  182. }
  183. if (nread == 0) {
  184. goto disconnect;
  185. }
  186. //printd("> %s\n", buf);
  187. if (io->read_cb) {
  188. //printd("read_cb------\n");
  189. io->read_cb(io, buf, nread);
  190. //printd("read_cb======\n");
  191. }
  192. if (nread == len) {
  193. goto read;
  194. }
  195. return;
  196. read_error:
  197. disconnect:
  198. hclose(io);
  199. }
  200. static void nio_write(hio_t* io) {
  201. //printd("nio_write fd=%d\n", io->fd);
  202. int nwrite = 0;
  203. write:
  204. if (write_queue_empty(&io->write_queue)) {
  205. return;
  206. }
  207. offset_buf_t* pbuf = write_queue_front(&io->write_queue);
  208. char* buf = pbuf->base + pbuf->offset;
  209. int len = pbuf->len - pbuf->offset;
  210. switch (io->io_type) {
  211. #ifdef WITH_OPENSSL
  212. case HIO_TYPE_SSL:
  213. nwrite = SSL_write((SSL*)io->ssl, buf, len);
  214. break;
  215. #endif
  216. case HIO_TYPE_TCP:
  217. #ifdef OS_UNIX
  218. nwrite = write(io->fd, buf, len);
  219. #else
  220. nwrite = send(io->fd, buf, len, 0);
  221. #endif
  222. break;
  223. case HIO_TYPE_UDP:
  224. case HIO_TYPE_IP:
  225. nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, sizeof(struct sockaddr_in6));
  226. break;
  227. default:
  228. nwrite = write(io->fd, buf, len);
  229. break;
  230. }
  231. //printd("write retval=%d\n", nwrite);
  232. if (nwrite < 0) {
  233. if (socket_errno() == EAGAIN) {
  234. //goto write_done;
  235. return;
  236. }
  237. else {
  238. io->error = socket_errno();
  239. perror("write");
  240. goto write_error;
  241. }
  242. }
  243. if (nwrite == 0) {
  244. goto disconnect;
  245. }
  246. if (io->write_cb) {
  247. //printd("write_cb------\n");
  248. io->write_cb(io, buf, nwrite);
  249. //printd("write_cb======\n");
  250. }
  251. pbuf->offset += nwrite;
  252. if (nwrite == len) {
  253. SAFE_FREE(pbuf->base);
  254. write_queue_pop_front(&io->write_queue);
  255. // write next
  256. goto write;
  257. }
  258. return;
  259. write_error:
  260. disconnect:
  261. hclose(io);
  262. }
  263. static void hio_handle_events(hio_t* io) {
  264. if ((io->events & READ_EVENT) && (io->revents & READ_EVENT)) {
  265. if (io->accept) {
  266. nio_accept(io);
  267. }
  268. else {
  269. nio_read(io);
  270. }
  271. }
  272. if ((io->events & WRITE_EVENT) && (io->revents & WRITE_EVENT)) {
  273. // NOTE: del WRITE_EVENT, if write_queue empty
  274. if (write_queue_empty(&io->write_queue)) {
  275. iowatcher_del_event(io->loop, io->fd, WRITE_EVENT);
  276. io->events &= ~WRITE_EVENT;
  277. }
  278. if (io->connect) {
  279. // NOTE: connect just do once
  280. // ONESHOT
  281. io->connect = 0;
  282. nio_connect(io);
  283. }
  284. else {
  285. nio_write(io);
  286. }
  287. }
  288. io->revents = 0;
  289. }
  290. int hio_accept(hio_t* io) {
  291. hio_add(io, hio_handle_events, READ_EVENT);
  292. return 0;
  293. }
  294. int hio_connect(hio_t* io) {
  295. int ret = connect(io->fd, io->peeraddr, sizeof(struct sockaddr_in6));
  296. #ifdef OS_WIN
  297. if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
  298. #else
  299. if (ret < 0 && socket_errno() != EINPROGRESS) {
  300. #endif
  301. perror("connect");
  302. hclose(io);
  303. return ret;
  304. }
  305. if (ret == 0) {
  306. // connect ok
  307. if (io->connect_cb) {
  308. io->connect_cb(io);
  309. }
  310. return 0;
  311. }
  312. return hio_add(io, hio_handle_events, WRITE_EVENT);
  313. }
  314. int hio_read (hio_t* io) {
  315. return hio_add(io, hio_handle_events, READ_EVENT);
  316. }
  317. int hio_write (hio_t* io, const void* buf, size_t len) {
  318. int nwrite = 0;
  319. if (write_queue_empty(&io->write_queue)) {
  320. try_write:
  321. switch (io->io_type) {
  322. #ifdef WITH_OPENSSL
  323. case HIO_TYPE_SSL:
  324. nwrite = SSL_write((SSL*)io->ssl, buf, len);
  325. break;
  326. #endif
  327. case HIO_TYPE_TCP:
  328. #ifdef OS_UNIX
  329. nwrite = write(io->fd, buf, len);
  330. #else
  331. nwrite = send(io->fd, buf, len, 0);
  332. #endif
  333. break;
  334. case HIO_TYPE_UDP:
  335. case HIO_TYPE_IP:
  336. nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, sizeof(struct sockaddr_in6));
  337. break;
  338. default:
  339. nwrite = write(io->fd, buf, len);
  340. break;
  341. }
  342. //printd("write retval=%d\n", nwrite);
  343. if (nwrite < 0) {
  344. if (socket_errno() == EAGAIN) {
  345. nwrite = 0;
  346. hlogw("try_write failed, enqueue!");
  347. goto enqueue;
  348. }
  349. else {
  350. perror("write");
  351. io->error = socket_errno();
  352. goto write_error;
  353. }
  354. }
  355. if (nwrite == 0) {
  356. goto disconnect;
  357. }
  358. if (io->write_cb) {
  359. //printd("try_write_cb------\n");
  360. io->write_cb(io, buf, nwrite);
  361. //printd("try_write_cb======\n");
  362. }
  363. if (nwrite == len) {
  364. //goto write_done;
  365. return nwrite;
  366. }
  367. hio_add(io, hio_handle_events, WRITE_EVENT);
  368. }
  369. enqueue:
  370. if (nwrite < len) {
  371. offset_buf_t rest;
  372. rest.len = len;
  373. rest.offset = nwrite;
  374. // NOTE: free in nio_write
  375. SAFE_ALLOC(rest.base, rest.len);
  376. memcpy(rest.base, (char*)buf, rest.len);
  377. if (io->write_queue.maxsize == 0) {
  378. write_queue_init(&io->write_queue, 4);
  379. }
  380. write_queue_push_back(&io->write_queue, &rest);
  381. }
  382. return nwrite;
  383. write_error:
  384. disconnect:
  385. hclose(io);
  386. return nwrite;
  387. }
  388. int hio_close (hio_t* io) {
  389. #ifdef OS_UNIX
  390. close(io->fd);
  391. #else
  392. closesocket(io->fd);
  393. #endif
  394. #ifdef WITH_OPENSSL
  395. if (io->ssl) {
  396. SSL_free((SSL*)io->ssl);
  397. }
  398. #endif
  399. return 0;
  400. }
  401. #endif