1 #include "private-libwebsockets.h"
7 * included from libwebsockets.c for unix builds
10 unsigned long long time_in_microseconds(void)
13 gettimeofday(&tv, NULL);
14 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
18 lws_get_random(struct lws_context *context, void *buf, int len)
20 return read(context->fd_random, (char *)buf, len);
24 lws_send_pipe_choked(struct lws *wsi)
26 struct lws_pollfd fds;
28 /* treat the fact we got a truncated send pending as if we're choked */
36 if (poll(&fds, 1, 0) != 1)
39 if ((fds.revents & POLLOUT) == 0)
42 /* okay to send another packet without blocking */
48 lws_poll_listen_fd(struct lws_pollfd *fd)
50 return poll(fd, 1, 0);
54 * This is just used to interrupt poll waiting
55 * we don't have to do anything with it.
63 * lws_cancel_service_pt() - Cancel servicing of pending socket activity
65 * @wsi: Cancel service on the thread this wsi is serviced by
67 * This function let a call to lws_service() waiting for a timeout
71 lws_cancel_service_pt(struct lws *wsi)
73 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
76 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
77 lwsl_err("Cannot write to dummy pipe");
81 * lws_cancel_service() - Cancel ALL servicing of pending socket activity
82 * @context: Websocket context
84 * This function let a call to lws_service() waiting for a timeout
88 lws_cancel_service(struct lws_context *context)
90 struct lws_context_per_thread *pt = &context->pt[0];
91 char buf = 0, m = context->count_threads;
94 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
95 lwsl_err("Cannot write to dummy pipe");
100 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
102 int syslog_level = LOG_DEBUG;
106 syslog_level = LOG_ERR;
109 syslog_level = LOG_WARNING;
112 syslog_level = LOG_NOTICE;
115 syslog_level = LOG_INFO;
118 syslog(syslog_level, "%s", line);
122 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
124 struct lws_context_per_thread *pt = &context->pt[tsi];
128 #ifdef LWS_OPENSSL_SUPPORT
129 struct lws *wsi_next;
132 /* stay dead once we are dead */
137 lws_libev_run(context, tsi);
138 lws_libuv_run(context, tsi);
140 if (!context->service_tid_detected) {
143 memset(&_lws, 0, sizeof(_lws));
144 _lws.context = context;
146 context->service_tid_detected = context->protocols[0].callback(
147 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
149 context->service_tid = context->service_tid_detected;
151 /* if we know we are draining rx ext, do not wait in poll */
152 if (pt->rx_draining_ext_list)
155 #ifdef LWS_OPENSSL_SUPPORT
156 /* if we know we have non-network pending data, do not wait in poll */
157 if (lws_ssl_anybody_has_buffered_read_tsi(context, tsi)) {
159 lwsl_err("ssl buffered read\n");
163 n = poll(pt->fds, pt->fds_count, timeout_ms);
165 #ifdef LWS_OPENSSL_SUPPORT
166 if (!pt->rx_draining_ext_list &&
167 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
169 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
171 lws_service_fd_tsi(context, NULL, tsi);
176 if (LWS_ERRNO != LWS_EINTR)
182 * For all guys with already-available ext data to drain, if they are
183 * not flowcontrolled, fake their POLLIN status
185 wsi = pt->rx_draining_ext_list;
187 pt->fds[wsi->position_in_fds_table].revents |=
188 pt->fds[wsi->position_in_fds_table].events & POLLIN;
189 wsi = wsi->u.ws.rx_draining_ext_list;
192 #ifdef LWS_OPENSSL_SUPPORT
194 * For all guys with buffered SSL read data already saved up, if they
195 * are not flowcontrolled, fake their POLLIN status so they'll get
196 * service to use up the buffered incoming data, even though their
197 * network socket may have nothing
200 wsi = pt->pending_read_list;
202 wsi_next = wsi->pending_read_list_next;
203 pt->fds[wsi->position_in_fds_table].revents |=
204 pt->fds[wsi->position_in_fds_table].events & POLLIN;
205 if (pt->fds[wsi->position_in_fds_table].revents & POLLIN)
207 * he's going to get serviced now, take him off the
208 * list of guys with buffered SSL. If he still has some
209 * at the end of the service, he'll get put back on the
212 lws_ssl_remove_wsi_from_buffered_list(wsi);
218 /* any socket with events to service? */
221 for (n = 0; n < pt->fds_count && c; n++) {
222 if (!pt->fds[n].revents)
227 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
228 if (read(pt->fds[n].fd, &buf, 1) != 1)
229 lwsl_err("Cannot read from dummy pipe.");
233 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
236 /* if something closed, retry this slot */
245 lws_plat_service(struct lws_context *context, int timeout_ms)
247 return lws_plat_service_tsi(context, timeout_ms, 0);
251 lws_plat_set_socket_options(struct lws_context *context, int fd)
254 socklen_t optlen = sizeof(optval);
256 #if defined(__APPLE__) || \
257 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
258 defined(__NetBSD__) || \
260 struct protoent *tcp_proto;
263 if (context->ka_time) {
264 /* enable keepalive on this socket */
266 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
267 (const void *)&optval, optlen) < 0)
270 #if defined(__APPLE__) || \
271 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
272 defined(__NetBSD__) || \
273 defined(__CYGWIN__) || defined(__OpenBSD__)
276 * didn't find a way to set these per-socket, need to
277 * tune kernel systemwide values
280 /* set the keepalive conditions we want on it too */
281 optval = context->ka_time;
282 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
283 (const void *)&optval, optlen) < 0)
286 optval = context->ka_interval;
287 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
288 (const void *)&optval, optlen) < 0)
291 optval = context->ka_probes;
292 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
293 (const void *)&optval, optlen) < 0)
300 #if !defined(__APPLE__) && \
301 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
302 !defined(__NetBSD__) && \
303 !defined(__OpenBSD__)
304 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
307 tcp_proto = getprotobyname("TCP");
308 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
312 /* We are nonblocking... */
313 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
320 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
322 if (info->uid != -1) {
323 struct passwd *p = getpwuid(info->uid);
326 initgroups(p->pw_name, info->gid);
327 if (setuid(info->uid))
328 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
330 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
332 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
335 if (setgid(info->gid))
336 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
341 sigpipe_handler(int x)
346 lws_plat_context_early_init(void)
350 signal(SIGUSR2, lws_sigusr2);
352 sigaddset(&mask, SIGUSR2);
354 sigprocmask(SIG_BLOCK, &mask, NULL);
356 signal(SIGPIPE, sigpipe_handler);
362 lws_plat_context_early_destroy(struct lws_context *context)
367 lws_plat_context_late_destroy(struct lws_context *context)
369 struct lws_context_per_thread *pt = &context->pt[0];
370 int m = context->count_threads;
372 if (context->lws_lookup)
373 lws_free(context->lws_lookup);
376 close(pt->dummy_pipe_fds[0]);
377 close(pt->dummy_pipe_fds[1]);
380 close(context->fd_random);
383 /* cast a struct sockaddr_in6 * into addr for ipv6 */
386 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr, size_t addrlen)
393 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
397 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
401 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
403 if (strcmp(ifc->ifa_name, ifname))
406 switch (ifc->ifa_addr->sa_family) {
410 /* map IPv4 to IPv6 */
411 bzero((char *)&addr6->sin6_addr,
412 sizeof(struct in6_addr));
413 addr6->sin6_addr.s6_addr[10] = 0xff;
414 addr6->sin6_addr.s6_addr[11] = 0xff;
415 memcpy(&addr6->sin6_addr.s6_addr[12],
416 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
417 sizeof(struct in_addr));
421 (struct sockaddr_in *)ifc->ifa_addr,
422 sizeof(struct sockaddr_in));
426 memcpy(&addr6->sin6_addr,
427 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
428 sizeof(struct in6_addr));
440 /* check if bind to IP adddress */
442 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
446 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
454 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
456 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
458 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
459 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
461 pt->fds[pt->fds_count++].revents = 0;
465 lws_plat_delete_socket_from_fds(struct lws_context *context,
466 struct lws *wsi, int m)
468 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
473 lws_plat_service_periodic(struct lws_context *context)
475 /* if our parent went down, don't linger around */
476 if (context->started_with_parent &&
477 kill(context->started_with_parent, 0) < 0)
478 kill(getpid(), SIGTERM);
482 lws_plat_change_pollfd(struct lws_context *context,
483 struct lws *wsi, struct lws_pollfd *pfd)
488 LWS_VISIBLE const char *
489 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
491 return inet_ntop(af, src, dst, cnt);
494 static lws_filefd_type
495 _lws_plat_file_open(struct lws *wsi, const char *filename,
496 unsigned long *filelen, int flags)
498 struct stat stat_buf;
499 int ret = open(filename, flags, 0664);
502 return LWS_INVALID_FILE;
504 if (fstat(ret, &stat_buf) < 0) {
506 return LWS_INVALID_FILE;
508 *filelen = stat_buf.st_size;
513 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
519 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
521 return lseek(fd, offset, SEEK_CUR);
525 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
526 unsigned char *buf, unsigned long len)
530 n = read((int)fd, buf, len);
542 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
543 unsigned char *buf, unsigned long len)
547 n = write((int)fd, buf, len);
559 lws_plat_init(struct lws_context *context,
560 struct lws_context_creation_info *info)
562 struct lws_context_per_thread *pt = &context->pt[0];
563 int n = context->count_threads, fd;
565 /* master context has the global fd lookup array */
566 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
568 if (context->lws_lookup == NULL) {
569 lwsl_err("OOM on lws_lookup array for %d connections\n",
574 lwsl_notice(" mem: platform fd map: %5u bytes\n",
575 sizeof(struct lws *) * context->max_fds);
576 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
578 context->fd_random = fd;
579 if (context->fd_random < 0) {
580 lwsl_err("Unable to open random device %s %d\n",
581 SYSTEM_RANDOM_FILEPATH, context->fd_random);
585 if (!lws_libev_init_fd_table(context) &&
586 !lws_libuv_init_fd_table(context)) {
587 /* otherwise libev handled it instead */
590 if (pipe(pt->dummy_pipe_fds)) {
591 lwsl_err("Unable to create pipe\n");
595 /* use the read end of pipe as first item */
596 pt->fds[0].fd = pt->dummy_pipe_fds[0];
597 pt->fds[0].events = LWS_POLLIN;
598 pt->fds[0].revents = 0;
604 context->fops.open = _lws_plat_file_open;
605 context->fops.close = _lws_plat_file_close;
606 context->fops.seek_cur = _lws_plat_file_seek_cur;
607 context->fops.read = _lws_plat_file_read;
608 context->fops.write = _lws_plat_file_write;