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 */
38 fds.fd = wsi->desc.sockfd;
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];
117 lws_stats_atomic_bump(context, pt, LWSSTATS_C_SERVICE_ENTRY, 1);
122 lws_libev_run(context, tsi);
123 lws_libuv_run(context, tsi);
124 lws_libevent_run(context, tsi);
126 if (!context->service_tid_detected) {
129 memset(&_lws, 0, sizeof(_lws));
130 _lws.context = context;
132 context->service_tid_detected =
133 context->vhost_list->protocols[0].callback(
134 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
136 context->service_tid = context->service_tid_detected;
139 * is there anybody with pending stuff that needs service forcing?
141 if (!lws_service_adjust_timeout(context, 1, tsi)) {
142 /* -1 timeout means just do forced service */
143 _lws_plat_service_tsi(context, -1, pt->tid);
144 /* still somebody left who wants forced service? */
145 if (!lws_service_adjust_timeout(context, 1, pt->tid))
146 /* yes... come back again quickly */
150 n = poll(pt->fds, pt->fds_count, timeout_ms);
152 #ifdef LWS_OPENSSL_SUPPORT
153 if (!pt->rx_draining_ext_list &&
154 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
156 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
158 lws_service_fd_tsi(context, NULL, tsi);
163 m = lws_service_flag_pending(context, tsi);
165 c = -1; /* unknown limit */
168 if (LWS_ERRNO != LWS_EINTR)
174 /* any socket with events to service? */
175 for (n = 0; n < pt->fds_count && c; n++) {
176 if (!pt->fds[n].revents)
181 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
182 if (read(pt->fds[n].fd, &buf, 1) != 1)
183 lwsl_err("Cannot read from dummy pipe.");
187 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
190 /* if something closed, retry this slot */
199 lws_plat_check_connection_error(struct lws *wsi)
205 lws_plat_service(struct lws_context *context, int timeout_ms)
207 return _lws_plat_service_tsi(context, timeout_ms, 0);
211 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
214 socklen_t optlen = sizeof(optval);
216 #if defined(__APPLE__) || \
217 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
218 defined(__NetBSD__) || \
220 struct protoent *tcp_proto;
223 if (vhost->ka_time) {
224 /* enable keepalive on this socket */
226 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
227 (const void *)&optval, optlen) < 0)
230 #if defined(__APPLE__) || \
231 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
232 defined(__NetBSD__) || \
233 defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
236 * didn't find a way to set these per-socket, need to
237 * tune kernel systemwide values
240 /* set the keepalive conditions we want on it too */
241 optval = vhost->ka_time;
242 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
243 (const void *)&optval, optlen) < 0)
246 optval = vhost->ka_interval;
247 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
248 (const void *)&optval, optlen) < 0)
251 optval = vhost->ka_probes;
252 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
253 (const void *)&optval, optlen) < 0)
261 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
263 #elif !defined(__APPLE__) && \
264 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
265 !defined(__NetBSD__) && \
266 !defined(__OpenBSD__)
267 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
270 tcp_proto = getprotobyname("TCP");
271 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
275 /* We are nonblocking... */
276 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
283 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
286 if (setgid(info->gid))
287 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
289 if (info->uid != -1) {
290 struct passwd *p = getpwuid(info->uid);
293 initgroups(p->pw_name, info->gid);
294 if (setuid(info->uid))
295 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
297 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
299 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
303 #ifdef LWS_WITH_PLUGINS
305 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
307 /* libuv.c implements these in a cross-platform way */
311 static int filter(const struct dirent *ent)
313 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
320 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
322 struct lws_plugin_capability lcaps;
323 struct lws_plugin *plugin;
324 lws_plugin_init_func initfunc;
325 struct dirent **namelist;
326 int n, i, m, ret = 0;
330 lwsl_notice(" Plugins:\n");
333 n = scandir(*d, &namelist, filter, alphasort);
335 lwsl_err("Scandir on %s failed\n", *d);
339 for (i = 0; i < n; i++) {
340 if (strlen(namelist[i]->d_name) < 7)
343 lwsl_notice(" %s\n", namelist[i]->d_name);
345 lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
346 namelist[i]->d_name);
347 l = dlopen(path, RTLD_NOW);
349 lwsl_err("Error loading DSO: %s\n", dlerror());
354 /* we could open it, can we get his init function? */
355 m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
356 namelist[i]->d_name + 3 /* snip lib... */);
357 path[m - 3] = '\0'; /* snip the .so */
358 initfunc = dlsym(l, path);
360 lwsl_err("Failed to get init on %s: %s",
361 namelist[i]->d_name, dlerror());
364 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
365 m = initfunc(context, &lcaps);
367 lwsl_err("Initializing %s failed %d\n",
368 namelist[i]->d_name, m);
373 plugin = lws_malloc(sizeof(*plugin));
378 plugin->list = context->plugin_list;
379 context->plugin_list = plugin;
380 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
381 plugin->name[sizeof(plugin->name) - 1] = '\0';
383 plugin->caps = lcaps;
384 context->plugin_protocol_count += lcaps.count_protocols;
385 context->plugin_extension_count += lcaps.count_extensions;
406 lws_plat_plugins_destroy(struct lws_context * context)
408 struct lws_plugin *plugin = context->plugin_list, *p;
409 lws_plugin_destroy_func func;
416 lwsl_notice("%s\n", __func__);
420 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
422 func = dlsym(plugin->l, path);
424 lwsl_err("Failed to get destroy on %s: %s",
425 plugin->name, dlerror());
430 lwsl_err("Initializing %s failed %d\n",
439 context->plugin_list = NULL;
450 sigabrt_handler(int x)
452 printf("%s\n", __func__);
458 lws_plat_context_early_init(void)
460 #if !defined(LWS_AVOID_SIGPIPE_IGN)
461 signal(SIGPIPE, SIG_IGN);
464 // signal(SIGABRT, sigabrt_handler);
470 lws_plat_context_early_destroy(struct lws_context *context)
475 lws_plat_context_late_destroy(struct lws_context *context)
477 struct lws_context_per_thread *pt = &context->pt[0];
478 int m = context->count_threads;
480 #ifdef LWS_WITH_PLUGINS
481 if (context->plugin_list)
482 lws_plat_plugins_destroy(context);
485 if (context->lws_lookup)
486 lws_free(context->lws_lookup);
489 if (pt->dummy_pipe_fds[0])
490 close(pt->dummy_pipe_fds[0]);
491 if (pt->dummy_pipe_fds[1])
492 close(pt->dummy_pipe_fds[1]);
495 if (!context->fd_random)
496 lwsl_err("ZERO RANDOM FD\n");
497 if (context->fd_random != LWS_INVALID_FILE)
498 close(context->fd_random);
501 /* cast a struct sockaddr_in6 * into addr for ipv6 */
504 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
512 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
516 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
520 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
522 if (strcmp(ifc->ifa_name, ifname))
525 switch (ifc->ifa_addr->sa_family) {
529 /* map IPv4 to IPv6 */
530 bzero((char *)&addr6->sin6_addr,
531 sizeof(struct in6_addr));
532 addr6->sin6_addr.s6_addr[10] = 0xff;
533 addr6->sin6_addr.s6_addr[11] = 0xff;
534 memcpy(&addr6->sin6_addr.s6_addr[12],
535 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
536 sizeof(struct in_addr));
540 (struct sockaddr_in *)ifc->ifa_addr,
541 sizeof(struct sockaddr_in));
545 memcpy(&addr6->sin6_addr,
546 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
547 sizeof(struct in6_addr));
559 /* check if bind to IP address */
561 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
565 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
573 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
575 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
577 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
578 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
579 lws_libevent_io(wsi, LWS_EV_START | LWS_EV_READ);
581 pt->fds[pt->fds_count++].revents = 0;
585 lws_plat_delete_socket_from_fds(struct lws_context *context,
586 struct lws *wsi, int m)
588 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
590 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
591 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
592 lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
598 lws_plat_service_periodic(struct lws_context *context)
600 /* if our parent went down, don't linger around */
601 if (context->started_with_parent &&
602 kill(context->started_with_parent, 0) < 0)
603 kill(getpid(), SIGTERM);
607 lws_plat_change_pollfd(struct lws_context *context,
608 struct lws *wsi, struct lws_pollfd *pfd)
613 LWS_VISIBLE const char *
614 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
616 return inet_ntop(af, src, dst, cnt);
619 LWS_VISIBLE lws_fop_fd_t
620 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
621 const char *vpath, lws_fop_flags_t *flags)
623 struct stat stat_buf;
624 int ret = open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
630 if (fstat(ret, &stat_buf) < 0)
633 fop_fd = malloc(sizeof(*fop_fd));
638 fop_fd->flags = *flags;
640 fop_fd->filesystem_priv = NULL; /* we don't use it */
641 fop_fd->len = stat_buf.st_size;
652 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
654 int fd = (*fop_fd)->fd;
662 LWS_VISIBLE lws_fileofs_t
663 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
667 if (offset > 0 && offset > fop_fd->len - fop_fd->pos)
668 offset = fop_fd->len - fop_fd->pos;
670 if ((lws_fileofs_t)fop_fd->pos + offset < 0)
671 offset = -fop_fd->pos;
673 r = lseek(fop_fd->fd, offset, SEEK_CUR);
678 lwsl_err("error seeking from cur %ld, offset %ld\n",
679 (long)fop_fd->pos, (long)offset);
685 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
686 uint8_t *buf, lws_filepos_t len)
690 n = read((int)fop_fd->fd, buf, len);
696 lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__, n,
697 (long)len, (long)fop_fd->pos, (long)fop_fd->len);
704 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
705 uint8_t *buf, lws_filepos_t len)
709 n = write((int)fop_fd->fd, buf, len);
723 lws_plat_init(struct lws_context *context,
724 struct lws_context_creation_info *info)
726 struct lws_context_per_thread *pt = &context->pt[0];
727 int n = context->count_threads, fd;
729 /* master context has the global fd lookup array */
730 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
732 if (context->lws_lookup == NULL) {
733 lwsl_err("OOM on lws_lookup array for %d connections\n",
738 lwsl_notice(" mem: platform fd map: %5lu bytes\n",
739 (unsigned long)(sizeof(struct lws *) * context->max_fds));
740 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
742 context->fd_random = fd;
743 if (context->fd_random < 0) {
744 lwsl_err("Unable to open random device %s %d\n",
745 SYSTEM_RANDOM_FILEPATH, context->fd_random);
749 if (!lws_libev_init_fd_table(context) &&
750 !lws_libuv_init_fd_table(context) &&
751 !lws_libevent_init_fd_table(context)) {
752 /* otherwise libev handled it instead */
755 if (pipe(pt->dummy_pipe_fds)) {
756 lwsl_err("Unable to create pipe\n");
760 /* use the read end of pipe as first item */
761 pt->fds[0].fd = pt->dummy_pipe_fds[0];
762 pt->fds[0].events = LWS_POLLIN;
763 pt->fds[0].revents = 0;
769 #ifdef LWS_WITH_PLUGINS
770 if (info->plugin_dirs)
771 lws_plat_plugins_init(context, info->plugin_dirs);