1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "private-lib-core.h"
26
27
28 int
lws_plat_pipe_create(struct lws * wsi)29 lws_plat_pipe_create(struct lws *wsi)
30 {
31 return 1;
32 }
33
34 int
lws_plat_pipe_signal(struct lws * wsi)35 lws_plat_pipe_signal(struct lws *wsi)
36 {
37 return 1;
38 }
39
40 void
lws_plat_pipe_close(struct lws * wsi)41 lws_plat_pipe_close(struct lws *wsi)
42 {
43 }
44
45 int
lws_send_pipe_choked(struct lws * wsi)46 lws_send_pipe_choked(struct lws *wsi)
47 {
48 struct lws *wsi_eff;
49
50 #if defined(LWS_WITH_HTTP2)
51 wsi_eff = lws_get_network_wsi(wsi);
52 #else
53 wsi_eff = wsi;
54 #endif
55
56 /* the fact we checked implies we avoided back-to-back writes */
57 wsi_eff->could_have_pending = 0;
58
59 /* treat the fact we got a truncated send pending as if we're choked */
60 if (lws_has_buffered_out(wsi_eff)
61 #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
62 || wsi->http.comp_ctx.buflist_comp ||
63 wsi->http.comp_ctx.may_have_more
64 #endif
65 )
66 return 1;
67
68 /* okay to send another packet without blocking */
69
70 return 0;
71 }
72
73 int
lws_poll_listen_fd(struct lws_pollfd * fd)74 lws_poll_listen_fd(struct lws_pollfd *fd)
75 {
76 // return poll(fd, 1, 0);
77
78 return 0;
79 }
80
81
82 int
_lws_plat_service_tsi(struct lws_context * context,int timeout_ms,int tsi)83 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
84 {
85 lws_usec_t timeout_us = timeout_ms * LWS_US_PER_MS;
86 struct lws_context_per_thread *pt;
87 int n = -1, m, c, a = 0;
88 //char buf;
89
90 /* stay dead once we are dead */
91
92 if (!context || !context->vhost_list)
93 return 1;
94
95 pt = &context->pt[tsi];
96
97 if (timeout_ms < 0)
98 timeout_ms = 0;
99 else
100 timeout_ms = 2000000000;
101
102 if (!pt->service_tid_detected) {
103 struct lws _lws;
104
105 memset(&_lws, 0, sizeof(_lws));
106 _lws.context = context;
107
108 pt->service_tid = context->vhost_list->protocols[0].callback(
109 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
110 pt->service_tid_detected = 1;
111 }
112
113 /*
114 * is there anybody with pending stuff that needs service forcing?
115 */
116 if (lws_service_adjust_timeout(context, 1, tsi)) {
117 again:
118 a = 0;
119 if (timeout_us) {
120 lws_usec_t us;
121
122 lws_pt_lock(pt, __func__);
123 /* don't stay in poll wait longer than next hr timeout */
124 us = __lws_sul_service_ripe(&pt->pt_sul_owner, lws_now_usecs());
125 if (us && us < timeout_us)
126 timeout_us = us;
127
128 lws_pt_unlock(pt);
129 }
130
131 n = poll(pt->fds, pt->fds_count, timeout_us / LWS_US_PER_MS);
132
133 m = 0;
134
135 if (pt->context->tls_ops &&
136 pt->context->tls_ops->fake_POLLIN_for_buffered)
137 m = pt->context->tls_ops->fake_POLLIN_for_buffered(pt);
138
139 if (/*!pt->ws.rx_draining_ext_list && */!m && !n) /* nothing to do */
140 return 0;
141 } else
142 a = 1;
143
144 m = lws_service_flag_pending(context, tsi);
145 if (m)
146 c = -1; /* unknown limit */
147 else
148 if (n < 0) {
149 if (LWS_ERRNO != LWS_EINTR)
150 return -1;
151 return 0;
152 } else
153 c = n;
154
155 /* any socket with events to service? */
156 for (n = 0; n < (int)pt->fds_count && c; n++) {
157 if (!pt->fds[n].revents)
158 continue;
159
160 c--;
161 #if 0
162 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
163 if (read(pt->fds[n].fd, &buf, 1) != 1)
164 lwsl_err("Cannot read from dummy pipe.");
165 continue;
166 }
167 #endif
168 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
169 if (m < 0)
170 return -1;
171 /* if something closed, retry this slot */
172 if (m)
173 n--;
174 }
175
176 if (a)
177 goto again;
178
179 return 0;
180 }
181
182 int
lws_plat_service(struct lws_context * context,int timeout_ms)183 lws_plat_service(struct lws_context *context, int timeout_ms)
184 {
185 return _lws_plat_service_tsi(context, timeout_ms, 0);
186 }
187
188 int
lws_plat_set_socket_options(struct lws_vhost * vhost,int fd,int unix_skt)189 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
190 {
191 return 0;
192 }
193
194
195 int
lws_plat_write_cert(struct lws_vhost * vhost,int is_key,int fd,void * buf,int len)196 lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
197 int len)
198 {
199 return 1;
200 }
201
202
203 /* cast a struct sockaddr_in6 * into addr for ipv6 */
204
205 int
lws_interface_to_sa(int ipv6,const char * ifname,struct sockaddr_in * addr,size_t addrlen)206 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
207 size_t addrlen)
208 {
209 return -1;
210 }
211
212 void
lws_plat_insert_socket_into_fds(struct lws_context * context,struct lws * wsi)213 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
214 {
215 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
216
217 pt->fds[pt->fds_count++].revents = 0;
218 }
219
220 void
lws_plat_delete_socket_from_fds(struct lws_context * context,struct lws * wsi,int m)221 lws_plat_delete_socket_from_fds(struct lws_context *context,
222 struct lws *wsi, int m)
223 {
224 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
225
226 pt->fds_count--;
227 }
228
229 int
lws_plat_change_pollfd(struct lws_context * context,struct lws * wsi,struct lws_pollfd * pfd)230 lws_plat_change_pollfd(struct lws_context *context,
231 struct lws *wsi, struct lws_pollfd *pfd)
232 {
233 return 0;
234 }
235
236 const char *
lws_plat_inet_ntop(int af,const void * src,char * dst,int cnt)237 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
238 {
239 //return inet_ntop(af, src, dst, cnt);
240 return "lws_plat_inet_ntop";
241 }
242
243 int
lws_plat_inet_pton(int af,const char * src,void * dst)244 lws_plat_inet_pton(int af, const char *src, void *dst)
245 {
246 //return inet_pton(af, src, dst);
247 return 1;
248 }
249
250
251