1 #include "private-libwebsockets.h"
6 #ifdef LWS_WITH_PLUGINS
13 * included from libwebsockets.c for unix builds
16 unsigned long long time_in_microseconds(void)
19 gettimeofday(&tv, NULL);
20 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
24 lws_get_random(struct lws_context *context, void *buf, int len)
26 return read(context->fd_random, (char *)buf, len);
30 lws_send_pipe_choked(struct lws *wsi)
32 struct lws_pollfd fds;
34 /* treat the fact we got a truncated send pending as if we're choked */
42 if (poll(&fds, 1, 0) != 1)
45 if ((fds.revents & POLLOUT) == 0)
48 /* okay to send another packet without blocking */
54 lws_poll_listen_fd(struct lws_pollfd *fd)
56 return poll(fd, 1, 0);
60 lws_cancel_service_pt(struct lws *wsi)
62 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
65 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
66 lwsl_err("Cannot write to dummy pipe");
70 lws_cancel_service(struct lws_context *context)
72 struct lws_context_per_thread *pt = &context->pt[0];
73 char buf = 0, m = context->count_threads;
76 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
77 lwsl_err("Cannot write to dummy pipe");
82 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
84 int syslog_level = LOG_DEBUG;
88 syslog_level = LOG_ERR;
91 syslog_level = LOG_WARNING;
94 syslog_level = LOG_NOTICE;
97 syslog_level = LOG_INFO;
100 syslog(syslog_level, "%s", line);
103 LWS_VISIBLE LWS_EXTERN int
104 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
106 struct lws_context_per_thread *pt;
110 /* stay dead once we are dead */
112 if (!context || !context->vhost_list)
115 pt = &context->pt[tsi];
120 lws_libev_run(context, tsi);
121 lws_libuv_run(context, tsi);
123 if (!context->service_tid_detected) {
126 memset(&_lws, 0, sizeof(_lws));
127 _lws.context = context;
129 context->service_tid_detected =
130 context->vhost_list->protocols[0].callback(
131 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
133 context->service_tid = context->service_tid_detected;
136 * is there anybody with pending stuff that needs service forcing?
138 if (!lws_service_adjust_timeout(context, 1, tsi)) {
139 /* -1 timeout means just do forced service */
140 _lws_plat_service_tsi(context, -1, pt->tid);
141 /* still somebody left who wants forced service? */
142 if (!lws_service_adjust_timeout(context, 1, pt->tid))
143 /* yes... come back again quickly */
147 n = poll(pt->fds, pt->fds_count, timeout_ms);
149 #ifdef LWS_OPENSSL_SUPPORT
150 if (!pt->rx_draining_ext_list &&
151 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
153 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
155 lws_service_fd_tsi(context, NULL, tsi);
160 m = lws_service_flag_pending(context, tsi);
162 c = -1; /* unknown limit */
165 if (LWS_ERRNO != LWS_EINTR)
171 /* any socket with events to service? */
172 for (n = 0; n < pt->fds_count && c; n++) {
173 if (!pt->fds[n].revents)
178 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
179 if (read(pt->fds[n].fd, &buf, 1) != 1)
180 lwsl_err("Cannot read from dummy pipe.");
184 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
187 /* if something closed, retry this slot */
196 lws_plat_check_connection_error(struct lws *wsi)
202 lws_plat_service(struct lws_context *context, int timeout_ms)
204 return _lws_plat_service_tsi(context, timeout_ms, 0);
208 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
211 socklen_t optlen = sizeof(optval);
213 #if defined(__APPLE__) || \
214 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
215 defined(__NetBSD__) || \
217 struct protoent *tcp_proto;
220 if (vhost->ka_time) {
221 /* enable keepalive on this socket */
223 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
224 (const void *)&optval, optlen) < 0)
227 #if defined(__APPLE__) || \
228 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
229 defined(__NetBSD__) || \
230 defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
233 * didn't find a way to set these per-socket, need to
234 * tune kernel systemwide values
237 /* set the keepalive conditions we want on it too */
238 optval = vhost->ka_time;
239 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
240 (const void *)&optval, optlen) < 0)
243 optval = vhost->ka_interval;
244 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
245 (const void *)&optval, optlen) < 0)
248 optval = vhost->ka_probes;
249 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
250 (const void *)&optval, optlen) < 0)
258 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
260 #elif !defined(__APPLE__) && \
261 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
262 !defined(__NetBSD__) && \
263 !defined(__OpenBSD__)
264 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
267 tcp_proto = getprotobyname("TCP");
268 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
272 /* We are nonblocking... */
273 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
280 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
283 if (setgid(info->gid))
284 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
286 if (info->uid != -1) {
287 struct passwd *p = getpwuid(info->uid);
290 initgroups(p->pw_name, info->gid);
291 if (setuid(info->uid))
292 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
294 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
296 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
300 #ifdef LWS_WITH_PLUGINS
302 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
304 /* libuv.c implements these in a cross-platform way */
308 static int filter(const struct dirent *ent)
310 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
317 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
319 struct lws_plugin_capability lcaps;
320 struct lws_plugin *plugin;
321 lws_plugin_init_func initfunc;
322 struct dirent **namelist;
323 int n, i, m, ret = 0;
327 lwsl_notice(" Plugins:\n");
330 n = scandir(*d, &namelist, filter, alphasort);
332 lwsl_err("Scandir on %s failed\n", *d);
336 for (i = 0; i < n; i++) {
337 if (strlen(namelist[i]->d_name) < 7)
340 lwsl_notice(" %s\n", namelist[i]->d_name);
342 lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
343 namelist[i]->d_name);
344 l = dlopen(path, RTLD_NOW);
346 lwsl_err("Error loading DSO: %s\n", dlerror());
351 /* we could open it, can we get his init function? */
352 m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
353 namelist[i]->d_name + 3 /* snip lib... */);
354 path[m - 3] = '\0'; /* snip the .so */
355 initfunc = dlsym(l, path);
357 lwsl_err("Failed to get init on %s: %s",
358 namelist[i]->d_name, dlerror());
361 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
362 m = initfunc(context, &lcaps);
364 lwsl_err("Initializing %s failed %d\n",
365 namelist[i]->d_name, m);
370 plugin = lws_malloc(sizeof(*plugin));
375 plugin->list = context->plugin_list;
376 context->plugin_list = plugin;
377 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
378 plugin->name[sizeof(plugin->name) - 1] = '\0';
380 plugin->caps = lcaps;
381 context->plugin_protocol_count += lcaps.count_protocols;
382 context->plugin_extension_count += lcaps.count_extensions;
403 lws_plat_plugins_destroy(struct lws_context * context)
405 struct lws_plugin *plugin = context->plugin_list, *p;
406 lws_plugin_destroy_func func;
413 lwsl_notice("%s\n", __func__);
417 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
419 func = dlsym(plugin->l, path);
421 lwsl_err("Failed to get destroy on %s: %s",
422 plugin->name, dlerror());
427 lwsl_err("Initializing %s failed %d\n",
436 context->plugin_list = NULL;
447 sigabrt_handler(int x)
449 printf("%s\n", __func__);
455 lws_plat_context_early_init(void)
457 signal(SIGPIPE, SIG_IGN);
459 // signal(SIGABRT, sigabrt_handler);
465 lws_plat_context_early_destroy(struct lws_context *context)
470 lws_plat_context_late_destroy(struct lws_context *context)
472 struct lws_context_per_thread *pt = &context->pt[0];
473 int m = context->count_threads;
475 #ifdef LWS_WITH_PLUGINS
476 if (context->plugin_list)
477 lws_plat_plugins_destroy(context);
480 if (context->lws_lookup)
481 lws_free(context->lws_lookup);
484 if (pt->dummy_pipe_fds[0])
485 close(pt->dummy_pipe_fds[0]);
486 if (pt->dummy_pipe_fds[1])
487 close(pt->dummy_pipe_fds[1]);
490 if (!context->fd_random)
491 lwsl_err("ZERO RANDOM FD\n");
492 if (context->fd_random != LWS_INVALID_FILE)
493 close(context->fd_random);
496 /* cast a struct sockaddr_in6 * into addr for ipv6 */
499 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
507 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
511 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
515 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
517 if (strcmp(ifc->ifa_name, ifname))
520 switch (ifc->ifa_addr->sa_family) {
524 /* map IPv4 to IPv6 */
525 bzero((char *)&addr6->sin6_addr,
526 sizeof(struct in6_addr));
527 addr6->sin6_addr.s6_addr[10] = 0xff;
528 addr6->sin6_addr.s6_addr[11] = 0xff;
529 memcpy(&addr6->sin6_addr.s6_addr[12],
530 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
531 sizeof(struct in_addr));
535 (struct sockaddr_in *)ifc->ifa_addr,
536 sizeof(struct sockaddr_in));
540 memcpy(&addr6->sin6_addr,
541 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
542 sizeof(struct in6_addr));
554 /* check if bind to IP address */
556 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
560 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
568 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
570 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
572 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
573 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
575 pt->fds[pt->fds_count++].revents = 0;
579 lws_plat_delete_socket_from_fds(struct lws_context *context,
580 struct lws *wsi, int m)
582 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
584 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
585 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
591 lws_plat_service_periodic(struct lws_context *context)
593 /* if our parent went down, don't linger around */
594 if (context->started_with_parent &&
595 kill(context->started_with_parent, 0) < 0)
596 kill(getpid(), SIGTERM);
600 lws_plat_change_pollfd(struct lws_context *context,
601 struct lws *wsi, struct lws_pollfd *pfd)
606 LWS_VISIBLE const char *
607 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
609 return inet_ntop(af, src, dst, cnt);
612 static lws_filefd_type
613 _lws_plat_file_open(struct lws *wsi, const char *filename,
614 unsigned long *filelen, int *flags)
616 struct stat stat_buf;
617 int ret = open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
620 return LWS_INVALID_FILE;
622 if (fstat(ret, &stat_buf) < 0) {
624 return LWS_INVALID_FILE;
626 *filelen = stat_buf.st_size;
631 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
637 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
639 return lseek(fd, offset, SEEK_CUR);
643 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
644 unsigned char *buf, unsigned long len)
648 n = read((int)fd, buf, len);
660 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
661 unsigned char *buf, unsigned long len)
665 n = write((int)fd, buf, len);
678 lws_plat_init(struct lws_context *context,
679 struct lws_context_creation_info *info)
681 struct lws_context_per_thread *pt = &context->pt[0];
682 int n = context->count_threads, fd;
684 /* master context has the global fd lookup array */
685 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
687 if (context->lws_lookup == NULL) {
688 lwsl_err("OOM on lws_lookup array for %d connections\n",
693 lwsl_notice(" mem: platform fd map: %5lu bytes\n",
694 (unsigned long)(sizeof(struct lws *) * context->max_fds));
695 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
697 context->fd_random = fd;
698 if (context->fd_random < 0) {
699 lwsl_err("Unable to open random device %s %d\n",
700 SYSTEM_RANDOM_FILEPATH, context->fd_random);
704 if (!lws_libev_init_fd_table(context) &&
705 !lws_libuv_init_fd_table(context)) {
706 /* otherwise libev handled it instead */
709 if (pipe(pt->dummy_pipe_fds)) {
710 lwsl_err("Unable to create pipe\n");
714 /* use the read end of pipe as first item */
715 pt->fds[0].fd = pt->dummy_pipe_fds[0];
716 pt->fds[0].events = LWS_POLLIN;
717 pt->fds[0].revents = 0;
723 context->fops.open = _lws_plat_file_open;
724 context->fops.close = _lws_plat_file_close;
725 context->fops.seek_cur = _lws_plat_file_seek_cur;
726 context->fops.read = _lws_plat_file_read;
727 context->fops.write = _lws_plat_file_write;
729 #ifdef LWS_WITH_PLUGINS
730 if (info->plugin_dirs)
731 lws_plat_plugins_init(context, info->plugin_dirs);