network.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #if defined(LWS_WITH_MBEDTLS)
  26. #if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
  27. #include "mbedtls/net_sockets.h"
  28. #else
  29. #include "mbedtls/net.h"
  30. #endif
  31. #endif
  32. int
  33. lws_plat_pipe_create(struct lws *wsi)
  34. {
  35. return 1;
  36. }
  37. int
  38. lws_plat_pipe_signal(struct lws *wsi)
  39. {
  40. return 1;
  41. }
  42. void
  43. lws_plat_pipe_close(struct lws *wsi)
  44. {
  45. }
  46. int
  47. lws_send_pipe_choked(struct lws *wsi)
  48. {
  49. struct lws *wsi_eff;
  50. #if defined(LWS_WITH_HTTP2)
  51. wsi_eff = lws_get_network_wsi(wsi);
  52. #else
  53. wsi_eff = wsi;
  54. #endif
  55. /* the fact we checked implies we avoided back-to-back writes */
  56. wsi_eff->could_have_pending = 0;
  57. /* treat the fact we got a truncated send pending as if we're choked */
  58. if (lws_has_buffered_out(wsi_eff)
  59. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  60. || wsi->http.comp_ctx.buflist_comp ||
  61. wsi->http.comp_ctx.may_have_more
  62. #endif
  63. )
  64. return 1;
  65. /* okay to send another packet without blocking */
  66. return 0;
  67. }
  68. int
  69. lws_poll_listen_fd(struct lws_pollfd *fd)
  70. {
  71. // return poll(fd, 1, 0);
  72. return 0;
  73. }
  74. int
  75. _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
  76. {
  77. lws_usec_t timeout_us = timeout_ms * LWS_US_PER_MS;
  78. struct lws_context_per_thread *pt;
  79. int n = -1, m, c, a = 0;
  80. //char buf;
  81. /* stay dead once we are dead */
  82. if (!context)
  83. return 1;
  84. pt = &context->pt[tsi];
  85. if (timeout_ms < 0)
  86. timeout_ms = 0;
  87. else
  88. timeout_ms = 2000000000;
  89. if (!pt->service_tid_detected && context->vhost_list) {
  90. struct lws _lws;
  91. memset(&_lws, 0, sizeof(_lws));
  92. _lws.context = context;
  93. pt->service_tid = context->vhost_list->protocols[0].callback(
  94. &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
  95. pt->service_tid_detected = 1;
  96. }
  97. /*
  98. * is there anybody with pending stuff that needs service forcing?
  99. */
  100. if (lws_service_adjust_timeout(context, 1, tsi)) {
  101. again:
  102. a = 0;
  103. if (timeout_us) {
  104. lws_usec_t us;
  105. lws_pt_lock(pt, __func__);
  106. /* don't stay in poll wait longer than next hr timeout */
  107. us = __lws_sul_service_ripe(pt->pt_sul_owner,
  108. LWS_COUNT_PT_SUL_OWNERS,
  109. lws_now_usecs());
  110. if (us && us < timeout_us)
  111. timeout_us = us;
  112. lws_pt_unlock(pt);
  113. }
  114. n = poll(pt->fds, pt->fds_count, timeout_us / LWS_US_PER_MS);
  115. m = 0;
  116. if (pt->context->tls_ops &&
  117. pt->context->tls_ops->fake_POLLIN_for_buffered)
  118. m = pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
  119. if (/*!pt->ws.rx_draining_ext_list && */!m && !n) /* nothing to do */
  120. return 0;
  121. } else
  122. a = 1;
  123. m = lws_service_flag_pending(context, tsi);
  124. if (m)
  125. c = -1; /* unknown limit */
  126. else
  127. if (n < 0) {
  128. if (LWS_ERRNO != LWS_EINTR)
  129. return -1;
  130. return 0;
  131. } else
  132. c = n;
  133. /* any socket with events to service? */
  134. for (n = 0; n < (int)pt->fds_count && c; n++) {
  135. if (!pt->fds[n].revents)
  136. continue;
  137. c--;
  138. #if 0
  139. if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
  140. if (read(pt->fds[n].fd, &buf, 1) != 1)
  141. lwsl_err("Cannot read from dummy pipe.");
  142. continue;
  143. }
  144. #endif
  145. m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
  146. if (m < 0)
  147. return -1;
  148. /* if something closed, retry this slot */
  149. if (m)
  150. n--;
  151. }
  152. if (a)
  153. goto again;
  154. return 0;
  155. }
  156. int
  157. lws_plat_service(struct lws_context *context, int timeout_ms)
  158. {
  159. return _lws_plat_service_tsi(context, timeout_ms, 0);
  160. }
  161. int
  162. lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
  163. {
  164. return 0;
  165. }
  166. int
  167. lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
  168. int len)
  169. {
  170. return 1;
  171. }
  172. /* cast a struct sockaddr_in6 * into addr for ipv6 */
  173. int
  174. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  175. size_t addrlen)
  176. {
  177. return -1;
  178. }
  179. void
  180. lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
  181. {
  182. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  183. pt->fds[pt->fds_count++].revents = 0;
  184. }
  185. void
  186. lws_plat_delete_socket_from_fds(struct lws_context *context,
  187. struct lws *wsi, int m)
  188. {
  189. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  190. pt->fds_count--;
  191. }
  192. int
  193. lws_plat_change_pollfd(struct lws_context *context,
  194. struct lws *wsi, struct lws_pollfd *pfd)
  195. {
  196. return 0;
  197. }
  198. const char *
  199. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
  200. {
  201. //return inet_ntop(af, src, dst, cnt);
  202. return "lws_plat_inet_ntop";
  203. }
  204. int
  205. lws_plat_inet_pton(int af, const char *src, void *dst)
  206. {
  207. //return inet_pton(af, src, dst);
  208. return 1;
  209. }
  210. #if defined(LWS_WITH_MBEDTLS)
  211. int
  212. lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
  213. {
  214. int fd = ((mbedtls_net_context *) ctx)->fd;
  215. int ret;
  216. if (fd < 0)
  217. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  218. ret = write(fd, buf, len);
  219. if (ret >= 0)
  220. return ret;
  221. if (errno == EAGAIN || errno == EWOULDBLOCK)
  222. return MBEDTLS_ERR_SSL_WANT_WRITE;
  223. if (errno == EPIPE || errno == ECONNRESET)
  224. return MBEDTLS_ERR_NET_CONN_RESET;
  225. if( errno == EINTR )
  226. return MBEDTLS_ERR_SSL_WANT_WRITE;
  227. return MBEDTLS_ERR_NET_SEND_FAILED;
  228. }
  229. int
  230. lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
  231. {
  232. int fd = ((mbedtls_net_context *) ctx)->fd;
  233. int ret;
  234. if (fd < 0)
  235. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  236. ret = (int)read(fd, buf, len);
  237. if (ret >= 0)
  238. return ret;
  239. if (errno == EAGAIN || errno == EWOULDBLOCK)
  240. return MBEDTLS_ERR_SSL_WANT_READ;
  241. if (errno == EPIPE || errno == ECONNRESET)
  242. return MBEDTLS_ERR_NET_CONN_RESET;
  243. if (errno == EINTR)
  244. return MBEDTLS_ERR_SSL_WANT_READ;
  245. return MBEDTLS_ERR_NET_RECV_FAILED;
  246. }
  247. #endif