ssl fix pending reads stuck in ssl layer
[platform/upstream/libwebsockets.git] / lib / lws-plat-unix.c
1 #include "private-libwebsockets.h"
2
3 /*
4  * included from libwebsockets.c for unix builds
5  */
6
7 unsigned long long time_in_microseconds(void)
8 {
9         struct timeval tv;
10         gettimeofday(&tv, NULL);
11         return (tv.tv_sec * 1000000) + tv.tv_usec;
12 }
13
14 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
15                                                              void *buf, int len)
16 {
17         return read(context->fd_random, (char *)buf, len);
18 }
19
20 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
21 {
22         struct libwebsocket_pollfd fds;
23
24         /* treat the fact we got a truncated send pending as if we're choked */
25         if (wsi->truncated_send_len)
26                 return 1;
27
28         fds.fd = wsi->sock;
29         fds.events = POLLOUT;
30         fds.revents = 0;
31
32         if (poll(&fds, 1, 0) != 1)
33                 return 1;
34
35         if ((fds.revents & POLLOUT) == 0)
36                 return 1;
37
38         /* okay to send another packet without blocking */
39
40         return 0;
41 }
42
43 LWS_VISIBLE int
44 lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
45 {
46         return poll(fd, 1, 0);
47 }
48
49 /*
50  * This is just used to interrupt poll waiting
51  * we don't have to do anything with it.
52  */
53 static void lws_sigusr2(int sig)
54 {
55 }
56
57 /**
58  * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
59  * @context:    Websocket context
60  *
61  *      This function let a call to libwebsocket_service() waiting for a timeout
62  *      immediately return.
63  */
64 LWS_VISIBLE void
65 libwebsocket_cancel_service(struct libwebsocket_context *context)
66 {
67         char buf = 0;
68
69         if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
70                 lwsl_err("Cannot write to dummy pipe");
71 }
72
73 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
74 {
75         int syslog_level = LOG_DEBUG;
76
77         switch (level) {
78         case LLL_ERR:
79                 syslog_level = LOG_ERR;
80                 break;
81         case LLL_WARN:
82                 syslog_level = LOG_WARNING;
83                 break;
84         case LLL_NOTICE:
85                 syslog_level = LOG_NOTICE;
86                 break;
87         case LLL_INFO:
88                 syslog_level = LOG_INFO;
89                 break;
90         }
91         syslog(syslog_level, "%s", line);
92 }
93
94 LWS_VISIBLE int
95 lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
96 {
97         int n;
98         int m;
99         char buf;
100
101         /* stay dead once we are dead */
102
103         if (!context)
104                 return 1;
105
106         lws_libev_run(context);
107
108         context->service_tid = context->protocols[0].callback(context, NULL,
109                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
110
111         n = poll(context->fds, context->fds_count, timeout_ms);
112         context->service_tid = 0;
113
114         if (n == 0) /* poll timeout */ {
115                 libwebsocket_service_fd(context, NULL);
116                 return 0;
117         }
118
119         if (n < 0) {
120                 if (LWS_ERRNO != LWS_EINTR)
121                         return -1;
122                 return 0;
123         }
124
125         /* any socket with events to service? */
126
127         for (n = 0; n < context->fds_count; n++) {
128 #ifdef LWS_OPENSSL_SUPPORT
129                 struct libwebsocket *wsi;
130                 
131                 wsi = context->lws_lookup[context->fds[n].fd];
132                 if (wsi == NULL)
133                         continue;
134                 /* 
135                  * if he's not flowcontrolled, make sure we service ssl
136                  * pending read data
137                  */
138                 if (wsi->ssl && wsi->buffered_reads_pending) {
139                         lwsl_debug("wsi %p: forcing POLLIN\n", wsi);
140                         context->fds[n].revents |= context->fds[n].events & POLLIN;
141                 }
142 #endif
143                 if (!context->fds[n].revents)
144                         continue;
145
146                 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
147                         if (read(context->fds[n].fd, &buf, 1) != 1)
148                                 lwsl_err("Cannot read from dummy pipe.");
149                         continue;
150                 }
151
152                 m = libwebsocket_service_fd(context, &context->fds[n]);
153                 if (m < 0)
154                         return -1;
155                 /* if something closed, retry this slot */
156                 if (m)
157                         n--;
158         }
159
160         return 0;
161 }
162
163 LWS_VISIBLE int
164 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
165 {
166         int optval = 1;
167         socklen_t optlen = sizeof(optval);
168
169 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
170     defined(__OpenBSD__)
171         struct protoent *tcp_proto;
172 #endif
173
174         if (context->ka_time) {
175                 /* enable keepalive on this socket */
176                 optval = 1;
177                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
178                                              (const void *)&optval, optlen) < 0)
179                         return 1;
180
181 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
182         defined(__CYGWIN__) || defined(__OpenBSD__)
183
184                 /*
185                  * didn't find a way to set these per-socket, need to
186                  * tune kernel systemwide values
187                  */
188 #else
189                 /* set the keepalive conditions we want on it too */
190                 optval = context->ka_time;
191                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
192                                              (const void *)&optval, optlen) < 0)
193                         return 1;
194
195                 optval = context->ka_interval;
196                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
197                                              (const void *)&optval, optlen) < 0)
198                         return 1;
199
200                 optval = context->ka_probes;
201                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
202                                              (const void *)&optval, optlen) < 0)
203                         return 1;
204 #endif
205         }
206
207         /* Disable Nagle */
208         optval = 1;
209 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
210     !defined(__OpenBSD__)
211         setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
212 #else
213         tcp_proto = getprotobyname("TCP");
214         setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
215 #endif
216
217         /* We are nonblocking... */
218         fcntl(fd, F_SETFL, O_NONBLOCK);
219
220         return 0;
221 }
222
223 LWS_VISIBLE void
224 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
225 {
226         if (info->gid != -1)
227                 if (setgid(info->gid))
228                         lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
229         if (info->uid != -1)
230                 if (setuid(info->uid))
231                         lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
232 }
233
234 LWS_VISIBLE int
235 lws_plat_init_fd_tables(struct libwebsocket_context *context)
236 {
237         if (lws_libev_init_fd_table(context))
238                 /* libev handled it instead */
239                 return 0;
240
241         if (pipe(context->dummy_pipe_fds)) {
242                 lwsl_err("Unable to create pipe\n");
243                 return 1;
244         }
245
246         /* use the read end of pipe as first item */
247         context->fds[0].fd = context->dummy_pipe_fds[0];
248         context->fds[0].events = LWS_POLLIN;
249         context->fds[0].revents = 0;
250         context->fds_count = 1;
251
252         context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
253         if (context->fd_random < 0) {
254                 lwsl_err("Unable to open random device %s %d\n",
255                                     SYSTEM_RANDOM_FILEPATH, context->fd_random);
256                 return 1;
257         }
258
259         return 0;
260 }
261
262 static void sigpipe_handler(int x)
263 {
264 }
265
266
267 LWS_VISIBLE int
268 lws_plat_context_early_init(void)
269 {
270         sigset_t mask;
271
272         signal(SIGUSR2, lws_sigusr2);
273         sigemptyset(&mask);
274         sigaddset(&mask, SIGUSR2);
275
276         sigprocmask(SIG_BLOCK, &mask, NULL);
277
278         signal(SIGPIPE, sigpipe_handler);
279
280         return 0;
281 }
282
283 LWS_VISIBLE void
284 lws_plat_context_early_destroy(struct libwebsocket_context *context)
285 {
286 }
287
288 LWS_VISIBLE void
289 lws_plat_context_late_destroy(struct libwebsocket_context *context)
290 {
291         close(context->dummy_pipe_fds[0]);
292         close(context->dummy_pipe_fds[1]);
293         close(context->fd_random);
294 }
295
296 /* cast a struct sockaddr_in6 * into addr for ipv6 */
297
298 LWS_VISIBLE int
299 interface_to_sa(struct libwebsocket_context *context,
300                 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
301 {
302         int rc = -1;
303
304         struct ifaddrs *ifr;
305         struct ifaddrs *ifc;
306 #ifdef LWS_USE_IPV6
307         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
308 #endif
309
310         getifaddrs(&ifr);
311         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
312                 if (!ifc->ifa_addr)
313                         continue;
314
315                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
316
317                 if (strcmp(ifc->ifa_name, ifname))
318                         continue;
319
320                 switch (ifc->ifa_addr->sa_family) {
321                 case AF_INET:
322 #ifdef LWS_USE_IPV6
323                         if (LWS_IPV6_ENABLED(context)) {
324                                 /* map IPv4 to IPv6 */
325                                 bzero((char *)&addr6->sin6_addr,
326                                                 sizeof(struct in6_addr));
327                                 addr6->sin6_addr.s6_addr[10] = 0xff;
328                                 addr6->sin6_addr.s6_addr[11] = 0xff;
329                                 memcpy(&addr6->sin6_addr.s6_addr[12],
330                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
331                                                         sizeof(struct in_addr));
332                         } else
333 #endif
334                                 memcpy(addr,
335                                         (struct sockaddr_in *)ifc->ifa_addr,
336                                                     sizeof(struct sockaddr_in));
337                         break;
338 #ifdef LWS_USE_IPV6
339                 case AF_INET6:
340                         if (rc >= 0)
341                                 break;
342                         memcpy(&addr6->sin6_addr,
343                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
344                                                        sizeof(struct in6_addr));
345                         break;
346 #endif
347                 default:
348                         continue;
349                 }
350                 rc = 0;
351         }
352
353         freeifaddrs(ifr);
354
355         if (rc == -1) {
356                 /* check if bind to IP adddress */
357 #ifdef LWS_USE_IPV6
358                 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
359                         rc = 0;
360                 else
361 #endif
362                 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
363                         rc = 0;
364         }
365
366         return rc;
367 }
368
369 LWS_VISIBLE void
370 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
371                                                        struct libwebsocket *wsi)
372 {
373         lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
374         context->fds[context->fds_count++].revents = 0;
375 }
376
377 LWS_VISIBLE void
378 lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
379                                                 struct libwebsocket *wsi, int m)
380 {
381 }
382
383 LWS_VISIBLE void
384 lws_plat_service_periodic(struct libwebsocket_context *context)
385 {
386         /* if our parent went down, don't linger around */
387         if (context->started_with_parent &&
388                               kill(context->started_with_parent, 0) < 0)
389                 kill(getpid(), SIGTERM);
390 }
391
392 LWS_VISIBLE int
393 lws_plat_change_pollfd(struct libwebsocket_context *context,
394                       struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
395 {
396         return 0;
397 }
398
399 LWS_VISIBLE int
400 lws_plat_open_file(const char* filename, unsigned long* filelen)
401 {
402         struct stat stat_buf;
403         int ret = open(filename, O_RDONLY);
404
405         if (ret < 0)
406                 return LWS_INVALID_FILE;
407
408         fstat(ret, &stat_buf);
409         *filelen = stat_buf.st_size;
410         return ret;
411 }
412
413 #ifdef LWS_USE_IPV6
414 LWS_VISIBLE const char *
415 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
416 {
417         return inet_ntop(af, src, dst, cnt);
418 }
419 #endif