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