nio.c 11 KB

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