unix-sockets.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #define _GNU_SOURCE
  22. #include "core/private.h"
  23. #include <pwd.h>
  24. #include <grp.h>
  25. int
  26. lws_send_pipe_choked(struct lws *wsi)
  27. {
  28. struct lws_pollfd fds;
  29. struct lws *wsi_eff;
  30. #if defined(LWS_WITH_HTTP2)
  31. wsi_eff = lws_get_network_wsi(wsi);
  32. #else
  33. wsi_eff = wsi;
  34. #endif
  35. /* the fact we checked implies we avoided back-to-back writes */
  36. wsi_eff->could_have_pending = 0;
  37. /* treat the fact we got a truncated send pending as if we're choked */
  38. if (lws_has_buffered_out(wsi_eff)
  39. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  40. ||wsi->http.comp_ctx.buflist_comp ||
  41. wsi->http.comp_ctx.may_have_more
  42. #endif
  43. )
  44. return 1;
  45. fds.fd = wsi_eff->desc.sockfd;
  46. fds.events = POLLOUT;
  47. fds.revents = 0;
  48. if (poll(&fds, 1, 0) != 1)
  49. return 1;
  50. if ((fds.revents & POLLOUT) == 0)
  51. return 1;
  52. /* okay to send another packet without blocking */
  53. return 0;
  54. }
  55. int
  56. lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
  57. {
  58. int optval = 1;
  59. socklen_t optlen = sizeof(optval);
  60. #if defined(__APPLE__) || \
  61. defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
  62. defined(__NetBSD__) || \
  63. defined(__OpenBSD__) || \
  64. defined(__HAIKU__)
  65. struct protoent *tcp_proto;
  66. #endif
  67. (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
  68. if (!unix_skt && vhost->ka_time) {
  69. /* enable keepalive on this socket */
  70. optval = 1;
  71. if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
  72. (const void *)&optval, optlen) < 0)
  73. return 1;
  74. #if defined(__APPLE__) || \
  75. defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
  76. defined(__NetBSD__) || \
  77. defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
  78. defined(__HAIKU__)
  79. /*
  80. * didn't find a way to set these per-socket, need to
  81. * tune kernel systemwide values
  82. */
  83. #else
  84. /* set the keepalive conditions we want on it too */
  85. #if defined(LWS_HAVE_TCP_USER_TIMEOUT)
  86. optval = 1000 * (vhost->ka_time +
  87. (vhost->ka_interval * vhost->ka_probes));
  88. if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
  89. (const void *)&optval, optlen) < 0)
  90. return 1;
  91. #endif
  92. optval = vhost->ka_time;
  93. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
  94. (const void *)&optval, optlen) < 0)
  95. return 1;
  96. optval = vhost->ka_interval;
  97. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
  98. (const void *)&optval, optlen) < 0)
  99. return 1;
  100. optval = vhost->ka_probes;
  101. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
  102. (const void *)&optval, optlen) < 0)
  103. return 1;
  104. #endif
  105. }
  106. #if defined(SO_BINDTODEVICE)
  107. if (!unix_skt && vhost->bind_iface && vhost->iface) {
  108. lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
  109. if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
  110. strlen(vhost->iface)) < 0) {
  111. lwsl_warn("Failed to bind to device %s\n", vhost->iface);
  112. return 1;
  113. }
  114. }
  115. #endif
  116. /* Disable Nagle */
  117. optval = 1;
  118. #if defined (__sun) || defined(__QNX__)
  119. if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
  120. return 1;
  121. #elif !defined(__APPLE__) && \
  122. !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
  123. !defined(__NetBSD__) && \
  124. !defined(__OpenBSD__) && \
  125. !defined(__HAIKU__)
  126. if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
  127. return 1;
  128. #else
  129. tcp_proto = getprotobyname("TCP");
  130. if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
  131. return 1;
  132. #endif
  133. /* We are nonblocking... */
  134. if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
  135. return 1;
  136. return 0;
  137. }
  138. /* cast a struct sockaddr_in6 * into addr for ipv6 */
  139. int
  140. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  141. size_t addrlen)
  142. {
  143. int rc = LWS_ITOSA_NOT_EXIST;
  144. struct ifaddrs *ifr;
  145. struct ifaddrs *ifc;
  146. #ifdef LWS_WITH_IPV6
  147. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  148. #endif
  149. getifaddrs(&ifr);
  150. for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
  151. if (!ifc->ifa_addr)
  152. continue;
  153. lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
  154. ifc->ifa_name, ifname,
  155. ifc->ifa_addr->sa_family, ipv6);
  156. if (strcmp(ifc->ifa_name, ifname))
  157. continue;
  158. switch (ifc->ifa_addr->sa_family) {
  159. #if defined(AF_PACKET)
  160. case AF_PACKET:
  161. /* interface exists but is not usable */
  162. rc = LWS_ITOSA_NOT_USABLE;
  163. continue;
  164. #endif
  165. case AF_INET:
  166. #ifdef LWS_WITH_IPV6
  167. if (ipv6) {
  168. /* map IPv4 to IPv6 */
  169. bzero((char *)&addr6->sin6_addr,
  170. sizeof(struct in6_addr));
  171. addr6->sin6_addr.s6_addr[10] = 0xff;
  172. addr6->sin6_addr.s6_addr[11] = 0xff;
  173. memcpy(&addr6->sin6_addr.s6_addr[12],
  174. &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
  175. sizeof(struct in_addr));
  176. } else
  177. #endif
  178. memcpy(addr,
  179. (struct sockaddr_in *)ifc->ifa_addr,
  180. sizeof(struct sockaddr_in));
  181. break;
  182. #ifdef LWS_WITH_IPV6
  183. case AF_INET6:
  184. memcpy(&addr6->sin6_addr,
  185. &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
  186. sizeof(struct in6_addr));
  187. break;
  188. #endif
  189. default:
  190. continue;
  191. }
  192. rc = LWS_ITOSA_USABLE;
  193. }
  194. freeifaddrs(ifr);
  195. if (rc) {
  196. /* check if bind to IP address */
  197. #ifdef LWS_WITH_IPV6
  198. if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
  199. rc = LWS_ITOSA_USABLE;
  200. else
  201. #endif
  202. if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
  203. rc = LWS_ITOSA_USABLE;
  204. }
  205. return rc;
  206. }
  207. const char *
  208. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
  209. {
  210. return inet_ntop(af, src, dst, cnt);
  211. }
  212. int
  213. lws_plat_inet_pton(int af, const char *src, void *dst)
  214. {
  215. return inet_pton(af, src, dst);
  216. }