1 #include "private-libwebsockets.h"
11 * included from libwebsockets.c for unix builds
14 unsigned long long time_in_microseconds(void)
17 gettimeofday(&tv, NULL);
18 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
22 lws_get_random(struct lws_context *context, void *buf, int len)
24 return read(context->fd_random, (char *)buf, len);
28 lws_send_pipe_choked(struct lws *wsi)
30 struct lws_pollfd fds;
32 /* treat the fact we got a truncated send pending as if we're choked */
40 if (poll(&fds, 1, 0) != 1)
43 if ((fds.revents & POLLOUT) == 0)
46 /* okay to send another packet without blocking */
52 lws_poll_listen_fd(struct lws_pollfd *fd)
54 return poll(fd, 1, 0);
58 * lws_cancel_service_pt() - Cancel servicing of pending socket activity
60 * @wsi: Cancel service on the thread this wsi is serviced by
62 * This function let a call to lws_service() waiting for a timeout
66 lws_cancel_service_pt(struct lws *wsi)
68 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
71 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
72 lwsl_err("Cannot write to dummy pipe");
76 * lws_cancel_service() - Cancel ALL servicing of pending socket activity
77 * @context: Websocket context
79 * This function let a call to lws_service() waiting for a timeout
83 lws_cancel_service(struct lws_context *context)
85 struct lws_context_per_thread *pt = &context->pt[0];
86 char buf = 0, m = context->count_threads;
89 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
90 lwsl_err("Cannot write to dummy pipe");
95 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
97 int syslog_level = LOG_DEBUG;
101 syslog_level = LOG_ERR;
104 syslog_level = LOG_WARNING;
107 syslog_level = LOG_NOTICE;
110 syslog_level = LOG_INFO;
113 syslog(syslog_level, "%s", line);
117 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
119 struct lws_context_per_thread *pt = &context->pt[tsi];
123 /* stay dead once we are dead */
125 if (!context || !context->vhost_list)
131 lws_libev_run(context, tsi);
132 lws_libuv_run(context, tsi);
134 if (!context->service_tid_detected) {
137 memset(&_lws, 0, sizeof(_lws));
138 _lws.context = context;
140 context->service_tid_detected =
141 context->vhost_list->protocols[0].callback(
142 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
144 context->service_tid = context->service_tid_detected;
146 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
148 n = poll(pt->fds, pt->fds_count, timeout_ms);
150 #ifdef LWS_OPENSSL_SUPPORT
151 if (!pt->rx_draining_ext_list &&
152 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
154 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
156 lws_service_fd_tsi(context, NULL, tsi);
161 m = lws_service_flag_pending(context, tsi);
163 c = -1; /* unknown limit */
166 if (LWS_ERRNO != LWS_EINTR)
172 /* any socket with events to service? */
173 for (n = 0; n < pt->fds_count && c; n++) {
174 if (!pt->fds[n].revents)
179 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
180 if (read(pt->fds[n].fd, &buf, 1) != 1)
181 lwsl_err("Cannot read from dummy pipe.");
185 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
188 /* if something closed, retry this slot */
197 lws_plat_service(struct lws_context *context, int timeout_ms)
199 return lws_plat_service_tsi(context, timeout_ms, 0);
203 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
206 socklen_t optlen = sizeof(optval);
208 #if defined(__APPLE__) || \
209 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
210 defined(__NetBSD__) || \
212 struct protoent *tcp_proto;
215 if (vhost->ka_time) {
216 /* enable keepalive on this socket */
218 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
219 (const void *)&optval, optlen) < 0)
222 #if defined(__APPLE__) || \
223 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
224 defined(__NetBSD__) || \
225 defined(__CYGWIN__) || defined(__OpenBSD__)
228 * didn't find a way to set these per-socket, need to
229 * tune kernel systemwide values
232 /* set the keepalive conditions we want on it too */
233 optval = vhost->ka_time;
234 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
235 (const void *)&optval, optlen) < 0)
238 optval = vhost->ka_interval;
239 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
240 (const void *)&optval, optlen) < 0)
243 optval = vhost->ka_probes;
244 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
245 (const void *)&optval, optlen) < 0)
252 #if !defined(__APPLE__) && \
253 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
254 !defined(__NetBSD__) && \
255 !defined(__OpenBSD__)
256 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
259 tcp_proto = getprotobyname("TCP");
260 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
264 /* We are nonblocking... */
265 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
272 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
275 if (setgid(info->gid))
276 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
278 if (info->uid != -1) {
279 struct passwd *p = getpwuid(info->uid);
282 initgroups(p->pw_name, info->gid);
283 if (setuid(info->uid))
284 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
286 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
288 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
292 #ifdef LWS_WITH_PLUGINS
294 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
296 /* libuv.c implements these in a cross-platform way */
300 static int filter(const struct dirent *ent)
302 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
309 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
311 struct lws_plugin_capability lcaps;
312 struct lws_plugin *plugin;
313 lws_plugin_init_func initfunc;
314 struct dirent **namelist;
315 int n, i, m, ret = 0;
319 lwsl_notice(" Plugins:\n");
322 n = scandir(*d, &namelist, filter, alphasort);
324 lwsl_err("Scandir on %s failed\n", *d);
328 for (i = 0; i < n; i++) {
329 if (strlen(namelist[i]->d_name) < 7)
332 lwsl_notice(" %s\n", namelist[i]->d_name);
334 snprintf(path, sizeof(path) - 1, "%s/%s", *d,
335 namelist[i]->d_name);
336 l = dlopen(path, RTLD_NOW);
338 lwsl_err("Error loading DSO: %s\n", dlerror());
343 /* we could open it, can we get his init function? */
344 m = snprintf(path, sizeof(path) - 1, "init_%s",
345 namelist[i]->d_name + 3 /* snip lib... */);
346 path[m - 3] = '\0'; /* snip the .so */
347 initfunc = dlsym(l, path);
349 lwsl_err("Failed to get init on %s: %s",
350 namelist[i]->d_name, dlerror());
353 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
354 m = initfunc(context, &lcaps);
356 lwsl_err("Initializing %s failed %d\n",
357 namelist[i]->d_name, m);
362 plugin = lws_malloc(sizeof(*plugin));
367 plugin->list = context->plugin_list;
368 context->plugin_list = plugin;
369 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
370 plugin->name[sizeof(plugin->name) - 1] = '\0';
372 plugin->caps = lcaps;
373 context->plugin_protocol_count += lcaps.count_protocols;
374 context->plugin_extension_count += lcaps.count_extensions;
395 lws_plat_plugins_destroy(struct lws_context * context)
397 struct lws_plugin *plugin = context->plugin_list, *p;
398 lws_plugin_destroy_func func;
405 lwsl_notice("%s\n", __func__);
409 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
411 func = dlsym(plugin->l, path);
413 lwsl_err("Failed to get destroy on %s: %s",
414 plugin->name, dlerror());
419 lwsl_err("Initializing %s failed %d\n",
428 context->plugin_list = NULL;
439 sigabrt_handler(int x)
441 printf("%s\n", __func__);
447 lws_plat_context_early_init(void)
449 signal(SIGPIPE, SIG_IGN);
451 // signal(SIGABRT, sigabrt_handler);
457 lws_plat_context_early_destroy(struct lws_context *context)
462 lws_plat_context_late_destroy(struct lws_context *context)
464 struct lws_context_per_thread *pt = &context->pt[0];
465 int m = context->count_threads;
467 #ifdef LWS_WITH_PLUGINS
468 if (context->plugin_list)
469 lws_plat_plugins_destroy(context);
472 if (context->lws_lookup)
473 lws_free(context->lws_lookup);
476 close(pt->dummy_pipe_fds[0]);
477 close(pt->dummy_pipe_fds[1]);
480 close(context->fd_random);
483 /* cast a struct sockaddr_in6 * into addr for ipv6 */
486 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
494 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
498 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
502 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
504 if (strcmp(ifc->ifa_name, ifname))
507 switch (ifc->ifa_addr->sa_family) {
511 /* map IPv4 to IPv6 */
512 bzero((char *)&addr6->sin6_addr,
513 sizeof(struct in6_addr));
514 addr6->sin6_addr.s6_addr[10] = 0xff;
515 addr6->sin6_addr.s6_addr[11] = 0xff;
516 memcpy(&addr6->sin6_addr.s6_addr[12],
517 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
518 sizeof(struct in_addr));
522 (struct sockaddr_in *)ifc->ifa_addr,
523 sizeof(struct sockaddr_in));
527 memcpy(&addr6->sin6_addr,
528 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
529 sizeof(struct in6_addr));
541 /* check if bind to IP adddress */
543 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
547 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
555 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
557 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
559 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
560 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
562 pt->fds[pt->fds_count++].revents = 0;
566 lws_plat_delete_socket_from_fds(struct lws_context *context,
567 struct lws *wsi, int m)
569 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
571 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
572 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
578 lws_plat_service_periodic(struct lws_context *context)
580 /* if our parent went down, don't linger around */
581 if (context->started_with_parent &&
582 kill(context->started_with_parent, 0) < 0)
583 kill(getpid(), SIGTERM);
587 lws_plat_change_pollfd(struct lws_context *context,
588 struct lws *wsi, struct lws_pollfd *pfd)
593 LWS_VISIBLE const char *
594 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
596 return inet_ntop(af, src, dst, cnt);
599 static lws_filefd_type
600 _lws_plat_file_open(struct lws *wsi, const char *filename,
601 unsigned long *filelen, int flags)
603 struct stat stat_buf;
604 int ret = open(filename, flags, 0664);
607 return LWS_INVALID_FILE;
609 if (fstat(ret, &stat_buf) < 0) {
611 return LWS_INVALID_FILE;
613 *filelen = stat_buf.st_size;
618 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
624 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
626 return lseek(fd, offset, SEEK_CUR);
630 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
631 unsigned char *buf, unsigned long len)
635 n = read((int)fd, buf, len);
647 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
648 unsigned char *buf, unsigned long len)
652 n = write((int)fd, buf, len);
665 lws_plat_init(struct lws_context *context,
666 struct lws_context_creation_info *info)
668 struct lws_context_per_thread *pt = &context->pt[0];
669 int n = context->count_threads, fd;
671 /* master context has the global fd lookup array */
672 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
674 if (context->lws_lookup == NULL) {
675 lwsl_err("OOM on lws_lookup array for %d connections\n",
680 lwsl_notice(" mem: platform fd map: %5u bytes\n",
681 sizeof(struct lws *) * context->max_fds);
682 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
684 context->fd_random = fd;
685 if (context->fd_random < 0) {
686 lwsl_err("Unable to open random device %s %d\n",
687 SYSTEM_RANDOM_FILEPATH, context->fd_random);
691 if (!lws_libev_init_fd_table(context) &&
692 !lws_libuv_init_fd_table(context)) {
693 /* otherwise libev handled it instead */
696 if (pipe(pt->dummy_pipe_fds)) {
697 lwsl_err("Unable to create pipe\n");
701 /* use the read end of pipe as first item */
702 pt->fds[0].fd = pt->dummy_pipe_fds[0];
703 pt->fds[0].events = LWS_POLLIN;
704 pt->fds[0].revents = 0;
710 context->fops.open = _lws_plat_file_open;
711 context->fops.close = _lws_plat_file_close;
712 context->fops.seek_cur = _lws_plat_file_seek_cur;
713 context->fops.read = _lws_plat_file_read;
714 context->fops.write = _lws_plat_file_write;
716 #ifdef LWS_WITH_PLUGINS
717 if (info->plugin_dirs)
718 lws_plat_plugins_init(context, info->plugin_dirs);