2 * libwebsockets - lib/plat/lws-plat-esp32.c
4 * Copyright (C) 2010-2017 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,
22 #include "core/private.h"
25 lws_send_pipe_choked(struct lws *wsi)
27 struct lws *wsi_eff = wsi;
29 struct timeval tv = { 0, 0 };
31 #if defined(LWS_WITH_HTTP2)
32 wsi_eff = lws_get_network_wsi(wsi);
35 /* the fact we checked implies we avoided back-to-back writes */
36 wsi_eff->could_have_pending = 0;
38 /* treat the fact we got a truncated send pending as if we're choked */
39 if (lws_has_buffered_out(wsi)
40 #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
41 || wsi->http.comp_ctx.buflist_comp ||
42 wsi->http.comp_ctx.may_have_more
48 FD_SET(wsi_eff->desc.sockfd, &writefds);
50 n = select(wsi_eff->desc.sockfd + 1, NULL, &writefds, NULL, &tv);
52 return 1; /* choked */
54 return !n; /* n = 0 = not writable = choked */
58 lws_poll_listen_fd(struct lws_pollfd *fd)
61 struct timeval tv = { 0, 0 };
64 FD_SET(fd->fd, &readfds);
66 return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
70 lws_plat_check_connection_error(struct lws *wsi)
76 lws_plat_set_nonblocking(int fd)
78 return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
82 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
85 socklen_t optlen = sizeof(optval);
87 #if defined(__APPLE__) || \
88 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
89 defined(__NetBSD__) || \
91 struct protoent *tcp_proto;
95 /* enable keepalive on this socket */
97 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
98 (const void *)&optval, optlen) < 0)
101 #if defined(__APPLE__) || \
102 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
103 defined(__NetBSD__) || \
104 defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
107 * didn't find a way to set these per-socket, need to
108 * tune kernel systemwide values
111 /* set the keepalive conditions we want on it too */
112 optval = vhost->ka_time;
113 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
114 (const void *)&optval, optlen) < 0)
117 optval = vhost->ka_interval;
118 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
119 (const void *)&optval, optlen) < 0)
122 optval = vhost->ka_probes;
123 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
124 (const void *)&optval, optlen) < 0)
131 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, optlen) < 0)
134 return lws_plat_set_nonblocking(fd);
137 /* cast a struct sockaddr_in6 * into addr for ipv6 */
140 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
144 int rc = LWS_ITOSA_NOT_EXIST;
149 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
153 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
157 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
159 if (strcmp(ifc->ifa_name, ifname))
162 switch (ifc->ifa_addr->sa_family) {
166 /* map IPv4 to IPv6 */
167 memset((char *)&addr6->sin6_addr, 0,
168 sizeof(struct in6_addr));
169 addr6->sin6_addr.s6_addr[10] = 0xff;
170 addr6->sin6_addr.s6_addr[11] = 0xff;
171 memcpy(&addr6->sin6_addr.s6_addr[12],
172 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
173 sizeof(struct in_addr));
177 (struct sockaddr_in *)ifc->ifa_addr,
178 sizeof(struct sockaddr_in));
182 memcpy(&addr6->sin6_addr,
183 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
184 sizeof(struct in6_addr));
190 rc = LWS_ITOSA_USABLE;
195 if (rc == LWS_ITOSA_NOT_EXIST) {
196 /* check if bind to IP address */
198 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
199 rc = LWS_ITOSA_USABLE;
202 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
203 rc = LWS_ITOSA_USABLE;
209 return LWS_ITOSA_NOT_EXIST;
213 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
215 return inet_ntop(af, src, dst, cnt);
219 lws_plat_inet_pton(int af, const char *src, void *dst)
221 return 1; // inet_pton(af, src, dst);