2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
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.
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.
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,
23 #include "core/private.h"
31 lws_send_pipe_choked(struct lws *wsi)
33 struct lws_pollfd fds;
36 #if !defined(LWS_WITHOUT_EXTENSIONS)
37 if (wsi->ws && wsi->ws->tx_draining_ext)
41 #if defined(LWS_WITH_HTTP2)
42 wsi_eff = lws_get_network_wsi(wsi);
47 /* the fact we checked implies we avoided back-to-back writes */
48 wsi_eff->could_have_pending = 0;
50 /* treat the fact we got a truncated send pending as if we're choked */
51 if (lws_has_buffered_out(wsi_eff)
52 #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
53 ||wsi->http.comp_ctx.buflist_comp ||
54 wsi->http.comp_ctx.may_have_more
59 fds.fd = wsi_eff->desc.sockfd;
63 if (poll(&fds, 1, 0) != 1)
66 if ((fds.revents & POLLOUT) == 0)
69 /* okay to send another packet without blocking */
75 lws_plat_set_nonblocking(int fd)
77 return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
81 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
84 socklen_t optlen = sizeof(optval);
86 #if defined(__APPLE__) || \
87 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
88 defined(__NetBSD__) || \
89 defined(__OpenBSD__) || \
91 struct protoent *tcp_proto;
94 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
96 if (!unix_skt && vhost->ka_time) {
97 /* enable keepalive on this socket */
99 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
100 (const void *)&optval, optlen) < 0)
103 #if defined(__APPLE__) || \
104 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
105 defined(__NetBSD__) || \
106 defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
110 * didn't find a way to set these per-socket, need to
111 * tune kernel systemwide values
114 /* set the keepalive conditions we want on it too */
116 #if defined(LWS_HAVE_TCP_USER_TIMEOUT)
117 optval = 1000 * (vhost->ka_time +
118 (vhost->ka_interval * vhost->ka_probes));
119 if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
120 (const void *)&optval, optlen) < 0)
123 optval = vhost->ka_time;
124 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
125 (const void *)&optval, optlen) < 0)
128 optval = vhost->ka_interval;
129 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
130 (const void *)&optval, optlen) < 0)
133 optval = vhost->ka_probes;
134 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
135 (const void *)&optval, optlen) < 0)
140 #if defined(SO_BINDTODEVICE)
141 if (!unix_skt && vhost->bind_iface && vhost->iface) {
142 lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
143 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
144 strlen(vhost->iface)) < 0) {
145 lwsl_warn("Failed to bind to device %s\n", vhost->iface);
153 #if defined (__sun) || defined(__QNX__)
154 if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
156 #elif !defined(__APPLE__) && \
157 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
158 !defined(__NetBSD__) && \
159 !defined(__OpenBSD__) && \
161 if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
164 tcp_proto = getprotobyname("TCP");
165 if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
169 return lws_plat_set_nonblocking(fd);
173 /* cast a struct sockaddr_in6 * into addr for ipv6 */
176 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
179 int rc = LWS_ITOSA_NOT_EXIST;
184 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
188 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
192 lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
193 ifc->ifa_name, ifname,
194 ifc->ifa_addr->sa_family, ipv6);
196 if (strcmp(ifc->ifa_name, ifname))
199 switch (ifc->ifa_addr->sa_family) {
200 #if defined(AF_PACKET)
202 /* interface exists but is not usable */
203 rc = LWS_ITOSA_NOT_USABLE;
210 /* map IPv4 to IPv6 */
211 memset((char *)&addr6->sin6_addr, 0,
212 sizeof(struct in6_addr));
213 addr6->sin6_addr.s6_addr[10] = 0xff;
214 addr6->sin6_addr.s6_addr[11] = 0xff;
215 memcpy(&addr6->sin6_addr.s6_addr[12],
216 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
217 sizeof(struct in_addr));
218 lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
222 (struct sockaddr_in *)ifc->ifa_addr,
223 sizeof(struct sockaddr_in));
227 memcpy(&addr6->sin6_addr,
228 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
229 sizeof(struct in6_addr));
235 rc = LWS_ITOSA_USABLE;
241 /* check if bind to IP address */
243 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
244 rc = LWS_ITOSA_USABLE;
247 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
248 rc = LWS_ITOSA_USABLE;
256 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
258 return inet_ntop(af, src, dst, cnt);
262 lws_plat_inet_pton(int af, const char *src, void *dst)
264 return inet_pton(af, src, dst);