nio.c 11 KB

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