Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / lib / plat / esp32 / esp32-sockets.c
1 /*
2  * libwebsockets - lib/plat/lws-plat-esp32.c
3  *
4  * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
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
22 #include "core/private.h"
23
24 int
25 lws_send_pipe_choked(struct lws *wsi)
26 {
27         struct lws *wsi_eff = wsi;
28         fd_set writefds;
29         struct timeval tv = { 0, 0 };
30         int n;
31 #if defined(LWS_WITH_HTTP2)
32         wsi_eff = lws_get_network_wsi(wsi);
33 #endif
34
35         /* the fact we checked implies we avoided back-to-back writes */
36         wsi_eff->could_have_pending = 0;
37
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
43 #endif
44         )
45                 return 1;
46
47         FD_ZERO(&writefds);
48         FD_SET(wsi_eff->desc.sockfd, &writefds);
49
50         n = select(wsi_eff->desc.sockfd + 1, NULL, &writefds, NULL, &tv);
51         if (n < 0)
52                 return 1; /* choked */
53
54         return !n; /* n = 0 = not writable = choked */
55 }
56
57 int
58 lws_poll_listen_fd(struct lws_pollfd *fd)
59 {
60         fd_set readfds;
61         struct timeval tv = { 0, 0 };
62
63         FD_ZERO(&readfds);
64         FD_SET(fd->fd, &readfds);
65
66         return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
67 }
68
69 int
70 lws_plat_check_connection_error(struct lws *wsi)
71 {
72         return 0;
73 }
74
75 int
76 lws_plat_set_nonblocking(int fd)
77 {
78         return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
79 }
80
81 int
82 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
83 {
84         int optval = 1;
85         socklen_t optlen = sizeof(optval);
86
87 #if defined(__APPLE__) || \
88     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
89     defined(__NetBSD__) || \
90     defined(__OpenBSD__)
91         struct protoent *tcp_proto;
92 #endif
93
94         if (vhost->ka_time) {
95                 /* enable keepalive on this socket */
96                 optval = 1;
97                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
98                                (const void *)&optval, optlen) < 0)
99                         return 1;
100
101 #if defined(__APPLE__) || \
102     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
103     defined(__NetBSD__) || \
104         defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
105
106                 /*
107                  * didn't find a way to set these per-socket, need to
108                  * tune kernel systemwide values
109                  */
110 #else
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)
115                         return 1;
116
117                 optval = vhost->ka_interval;
118                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
119                                (const void *)&optval, optlen) < 0)
120                         return 1;
121
122                 optval = vhost->ka_probes;
123                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
124                                (const void *)&optval, optlen) < 0)
125                         return 1;
126 #endif
127         }
128
129         /* Disable Nagle */
130         optval = 1;
131         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval, optlen) < 0)
132                 return 1;
133
134         return lws_plat_set_nonblocking(fd);
135 }
136
137 /* cast a struct sockaddr_in6 * into addr for ipv6 */
138
139 int
140 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
141                     size_t addrlen)
142 {
143 #if 0
144         int rc = LWS_ITOSA_NOT_EXIST;
145
146         struct ifaddrs *ifr;
147         struct ifaddrs *ifc;
148 #ifdef LWS_WITH_IPV6
149         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
150 #endif
151
152         getifaddrs(&ifr);
153         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
154                 if (!ifc->ifa_addr)
155                         continue;
156
157                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
158
159                 if (strcmp(ifc->ifa_name, ifname))
160                         continue;
161
162                 switch (ifc->ifa_addr->sa_family) {
163                 case AF_INET:
164 #ifdef LWS_WITH_IPV6
165                         if (ipv6) {
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));
174                         } else
175 #endif
176                                 memcpy(addr,
177                                         (struct sockaddr_in *)ifc->ifa_addr,
178                                                     sizeof(struct sockaddr_in));
179                         break;
180 #ifdef LWS_WITH_IPV6
181                 case AF_INET6:
182                         memcpy(&addr6->sin6_addr,
183                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
184                                                        sizeof(struct in6_addr));
185                         break;
186 #endif
187                 default:
188                         continue;
189                 }
190                 rc = LWS_ITOSA_USABLE;
191         }
192
193         freeifaddrs(ifr);
194
195         if (rc == LWS_ITOSA_NOT_EXIST) {
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
206         return rc;
207 #endif
208
209         return LWS_ITOSA_NOT_EXIST;
210 }
211
212 const char *
213 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
214 {
215         return inet_ntop(af, src, dst, cnt);
216 }
217
218 int
219 lws_plat_inet_pton(int af, const char *src, void *dst)
220 {
221         return 1; //  inet_pton(af, src, dst);
222 }
223
224
225
226