unix-sockets.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #ifdef LWS_WITH_IPV6
  61. optval = 0;
  62. setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (const void*)&optval, optlen);
  63. #endif
  64. #if defined(__APPLE__) || \
  65. defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
  66. defined(__NetBSD__) || \
  67. defined(__OpenBSD__) || \
  68. defined(__HAIKU__)
  69. struct protoent *tcp_proto;
  70. #endif
  71. (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
  72. if (!unix_skt && vhost->ka_time) {
  73. /* enable keepalive on this socket */
  74. optval = 1;
  75. if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
  76. (const void *)&optval, optlen) < 0)
  77. return 1;
  78. #if defined(__APPLE__) || \
  79. defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
  80. defined(__NetBSD__) || \
  81. defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
  82. defined(__HAIKU__)
  83. /*
  84. * didn't find a way to set these per-socket, need to
  85. * tune kernel systemwide values
  86. */
  87. #else
  88. /* set the keepalive conditions we want on it too */
  89. #if defined(LWS_HAVE_TCP_USER_TIMEOUT)
  90. optval = 1000 * (vhost->ka_time +
  91. (vhost->ka_interval * vhost->ka_probes));
  92. if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
  93. (const void *)&optval, optlen) < 0)
  94. return 1;
  95. #endif
  96. optval = vhost->ka_time;
  97. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
  98. (const void *)&optval, optlen) < 0)
  99. return 1;
  100. optval = vhost->ka_interval;
  101. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
  102. (const void *)&optval, optlen) < 0)
  103. return 1;
  104. optval = vhost->ka_probes;
  105. if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
  106. (const void *)&optval, optlen) < 0)
  107. return 1;
  108. #endif
  109. }
  110. #if defined(SO_BINDTODEVICE)
  111. if (!unix_skt && vhost->bind_iface && vhost->iface) {
  112. lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
  113. if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
  114. strlen(vhost->iface)) < 0) {
  115. lwsl_warn("Failed to bind to device %s\n", vhost->iface);
  116. return 1;
  117. }
  118. }
  119. #endif
  120. /* Disable Nagle */
  121. optval = 1;
  122. #if defined (__sun) || defined(__QNX__)
  123. if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
  124. return 1;
  125. #elif !defined(__APPLE__) && \
  126. !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
  127. !defined(__NetBSD__) && \
  128. !defined(__OpenBSD__) && \
  129. !defined(__HAIKU__)
  130. if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
  131. return 1;
  132. #else
  133. tcp_proto = getprotobyname("TCP");
  134. if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
  135. return 1;
  136. #endif
  137. /* We are nonblocking... */
  138. if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
  139. return 1;
  140. return 0;
  141. }
  142. /* cast a struct sockaddr_in6 * into addr for ipv6 */
  143. int
  144. lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
  145. size_t addrlen)
  146. {
  147. int rc = LWS_ITOSA_NOT_EXIST;
  148. struct ifaddrs *ifr;
  149. struct ifaddrs *ifc;
  150. #ifdef LWS_WITH_IPV6
  151. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  152. #endif
  153. getifaddrs(&ifr);
  154. for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
  155. if (!ifc->ifa_addr)
  156. continue;
  157. lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
  158. ifc->ifa_name, ifname,
  159. ifc->ifa_addr->sa_family, ipv6);
  160. if (strcmp(ifc->ifa_name, ifname))
  161. continue;
  162. switch (ifc->ifa_addr->sa_family) {
  163. #if defined(AF_PACKET)
  164. case AF_PACKET:
  165. /* interface exists but is not usable */
  166. rc = LWS_ITOSA_NOT_USABLE;
  167. continue;
  168. #endif
  169. case AF_INET:
  170. #ifdef LWS_WITH_IPV6
  171. if (ipv6) {
  172. /* map IPv4 to IPv6 */
  173. bzero((char *)&addr6->sin6_addr,
  174. sizeof(struct in6_addr));
  175. addr6->sin6_addr.s6_addr[10] = 0xff;
  176. addr6->sin6_addr.s6_addr[11] = 0xff;
  177. memcpy(&addr6->sin6_addr.s6_addr[12],
  178. &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
  179. sizeof(struct in_addr));
  180. } else
  181. #endif
  182. memcpy(addr,
  183. (struct sockaddr_in *)ifc->ifa_addr,
  184. sizeof(struct sockaddr_in));
  185. break;
  186. #ifdef LWS_WITH_IPV6
  187. case AF_INET6:
  188. memcpy(&addr6->sin6_addr,
  189. &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
  190. sizeof(struct in6_addr));
  191. break;
  192. #endif
  193. default:
  194. continue;
  195. }
  196. rc = LWS_ITOSA_USABLE;
  197. }
  198. freeifaddrs(ifr);
  199. if (rc) {
  200. /* check if bind to IP address */
  201. #ifdef LWS_WITH_IPV6
  202. if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
  203. rc = LWS_ITOSA_USABLE;
  204. else
  205. #endif
  206. if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
  207. rc = LWS_ITOSA_USABLE;
  208. }
  209. return rc;
  210. }
  211. const char *
  212. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
  213. {
  214. return inet_ntop(af, src, dst, cnt);
  215. }
  216. int
  217. lws_plat_inet_pton(int af, const char *src, void *dst)
  218. {
  219. return inet_pton(af, src, dst);
  220. }